Yii1.*与Yii2中配置路由规则rules是几乎是一样的,但还是有细微的差别。

在Yii1.*中开启path路由规则直接使用

'urlFormat' => 'path',

但在Yii2中已经没有urlFormat 对象方法,在Yii2取而代之的是

 'enablePrettyUrl'=>TRUE,

一个典型的Yii1.* urlManager配置:

  'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false, //隐藏index.php
'urlSuffix' => '.html', //后缀
'rules' => array(
'news/detail-<id:.*>' => 'news/detail', //http://cms.com/news/detail-27d24c26.0486c.0aea8.a3d2803b.0000111.03.html id==>[id] => 27d24c26.0486c.0aea8.a3d2803b.0000111.03
'news/<id:.*>-<category:\d+>' => 'news', //http://cms.com/news/02f5bc8f-04600-0b477-c6bc82ab-0000111-03-1.html ==== $this->createUrl('news/', array('id' => $value['id'],'category'=>1));
'singlePage/<id:.*>' => 'singlePage',
'contact' => 'about/contact',
'addOrder' => 'Online/addOrder',
/**
* $this->createUrl('news/index',array('userid'=>123,'title'=>3434,'nid'=>'sdfsdfsdf')) index.php/new/index?userid=123&title=3434&nid=sdfsdfsdfsd
* http://cms.com/news/123/3434/sdfsdfsdf-index.html
*/
'news/<id:\d+>/<title:.*?>/<nid:.*?>-index' => 'news/index',
'refresh/' => 'index/Refresh',
'index.jsp/' => 'index/',
'index.aspx/' => 'index/',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),

那Yii2如何配置呢?

首先我们的URL地址是这样的

http://new.com/index.php?r=auth%2Fmenulink&post=2

我们要让地址改为path模式:

http://new.com/auth/menulink/post/2

1.在Nginx中开启rewrite

server {
listen 80;
server_name new.com ;
location / {
root F:/www/new/web;
index index.html index.htm index.php;
#autoindex on;
if (!-e $request_filename){
rewrite ^/(.*) /index.php?r=$1 last;
}
}
location ~ \.php$ {
root F:/www/new/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

2.config中web.php 配置

 'urlManager' => [
'enablePrettyUrl' => true, //美化url==ture
'enableStrictParsing' => false, //不启用严格解析
'showScriptName' => false, //隐藏index.php
'rules' => [
'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'/m-api/' => '/meanpan-api/index', // 默认index
'/mp-api/<action>' => '/meanpan-api/<action>', // /mp-api/*==>/meanpan-api/*
],
]

参数说明:

Yii官网说明:http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html

$enablePrettyUrl 

Whether to enable pretty URLs. Instead of putting all parameters in the query string part of a URL, pretty URLs allow using path info to represent some of the parameters and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of "/index.php?r=news/view&id=100".

$enableStrictParsing

Whether to enable strict parsing. If strict parsing is enabled, the incoming requested URL must match at least one of the $rules in order to be treated as a valid request. Otherwise, the path info part of the request will be treated as the requested route. This property is used only when $enablePrettyUrl is true.

$showScriptName 

Whether to show entry script name in the constructed URL. Defaults to true. This property is used only if $enablePrettyUrl is true.

现在访问URL就成为path模式了。

启用开发模式DEBUG

 if (YII_ENV_DEV) {
// 启用开发模式DEBUG
$config['bootstrap'][] = 'debug';
$config['modules']['debug']['class'] = 'yii\debug\Module';
$config['modules']['debug']['allowedIPs'] =['127.0.0.1','201.104.104.104',"::1"];
$config['modules']['debug']['historySize'] =500; # 修改debug记录条数 $config['bootstrap'][] = 'gii';
$config['modules']['gii'] ['class']= 'yii\gii\Module';
$config['modules']['gii']['allowedIPs'] =['127.0.0.1','201.104.104.104',"::1"];
}

Yii2的urlManager URL美化的更多相关文章

  1. yii2中的url美化

    在yii2中,如果想实现类似于post/1,post/update/1之类的效果,官方文档已经有明确的说明 但是如果想把所有的controller都实现,这里采用yii1的方法 'rules' =&g ...

  2. yii2 url 美化参数

    所谓的url参数美化就是将冗长的字符串 进行正则替换 yii2 框架的url参数美化完成需要完成两个阶段 第一个阶段分apache和nginx 两种的配置 apache :1.1 必须开启rewrit ...

  3. yii2项目实战-路由美化以及如何正确的生成链接

    yii2项目实战-路由美化以及如何正确的生成链接 更新于 2016年12月17日 by 白狼 被浏览了 705 次 美化路由 何为美化路由呢?美化嘛,无外乎就是给路由化化妆,让她好看点.我虽没化过妆, ...

  4. Yii2实现即可以美化路由访问又可以原始路由访问

    1. 本地环境 nginx version: nginx/1.11.1 PHP 7.1.0-dev (cli) mysql Ver 14.14 Distrib 5.7.22, for Linux (x ...

  5. YIi url美化

    一.Yii Url美化,配置urlManager组件 'urlManager' => [ 'enablePrettyUrl' => true, // 开启URL美化,可以去掉 index. ...

  6. Yii2开启enableprettyurl(url美化)无效

    最终显示的url格式为:http://localhost/yii2/frontend/web/site/about 在/config/main.php中 'components'=>[] 中添加 ...

  7. yii url美化 urlManager组件

    yii的官方文档对此的解释如下: urlSuffix  此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null.例如可以将此设置为.html,让url看起来“像”是一 ...

  8. Yii2.0 URL美化

    1. 程序初始化注册文件,加入如下: 'urlManager' =>[ 'class' => 'yii\web\UrlManager', 'showScriptName' =>fal ...

  9. yii2.0 url美化-apache服务器

    //配置内容 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => false, //不启用严格 ...

随机推荐

  1. SQL 优化tips

    1. 陷阱, 1)几个表进行join,然后过滤 等价于 2)分别过滤为小表后,再join? 并不完全.2)确实比1)效率高,但要注意一些NULL值过滤.否则2)得到的结果比1)多

  2. html页面输入框input的美化

    input输入框是网页必不可少的组件,可是每个浏览器对于输入框的显示样式各有不同 例如:    上图分别就是谷歌浏览器和IE浏览器自带显示的输入框,样式也不足人意,所以大多都会自己写样式 以下是一个简 ...

  3. linux系统下sendmail的搭建

    学习鸟哥linux私房菜所得 sendmail 可以使用rpm -qa |grep sendmail来查看一下是否已安装sendmail-cf和sendmail 如果没有安装可用yum -y inst ...

  4. Java程序员

    从生存.制胜.发展三个方面入手,为大家展示出程序员求职与工作的一幅3D全景图像.本书中既有在公司中的生存技巧,又有高手达人的进阶策略,既有求职攻略的按图索骥,又有入职后生产环境的破解揭秘. 书中浓缩了 ...

  5. bandicam如何录制视频

    我们一般都很熟悉这类软件:屏幕录制专家和kk录制等,这些都是国内比较优秀的作品.不过exe的封装格式以及录制的清晰度让人很纠结.所以这里要为大家分享的是一款韩国人写录制软件Bandicam.Bandi ...

  6. 10月24日下午PHP封装

    class Ren { private $name; private $sex; private $age;//年龄必须在18-50岁之间 function __construct($n) { $th ...

  7. Google Map API V3开发(1)

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

  8. Java数据结构——字典树TRIE

    又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计. 它的优点是:利用字符串的公共 ...

  9. CSS选择器优先级总结

    CSS三大特性-- 继承. 优先级和层叠. 继承:即子类元素继承父类的样式; 优先级:是指不同类别样式的权重比较; 层叠:是说当数量相同时,通过层叠(后者覆盖前者)的样式. css选择符分类 首先来看 ...

  10. 在Application中集成Microsoft Translator服务之开发前准备

    第一步:准备一个微软账号 要使用Microsoft Translator API需要在Microsoft Azure Marketplace(https://datamarket.azure.com/ ...