Yii2的urlManager URL美化

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‘,            ],        ]

参数说明:

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

$enablePrettyUrlboolean $enablePrettyUrl =false

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".

$enableStrictParsingboolean $enableStrictParsing =false

Whether to enable strict parsing. If strict parsing is enabled, the incoming requested URL must match at least one of the$rulesin 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$enablePrettyUrlis true.

$showScriptNameboolean $showScriptName =true

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

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

时间: 2024-10-07 06:10:17

Yii2的urlManager URL美化的相关文章

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

yii2项目实战-路由美化以及如何正确的生成链接 更新于 2016年12月17日 by 白狼 被浏览了 705 次 美化路由 何为美化路由呢?美化嘛,无外乎就是给路由化化妆,让她好看点.我虽没化过妆,那好歹也是见过描描眉的.下面我们就来看看如何给你的路由添加添加点"颜色"的. yii的路由美化工作,全权由urlManager组件负责.默认情况下,该组件并没有开启. 我们在配置文件backend\config\main.php中简单配置下该组件 'urlManager' => [

yii url美化 urlManager组件

yii的官方文档对此的解释如下: urlSuffix  此规则使用的url后缀,默认使用CurlManger::urlSuffix,值为null.例如可以将此设置为.html,让url看起来“像”是一个静态页面. caseSensitive  是否大小写敏感,默认使用CUrlManager::caseSensitive,值为null. defaultParams  该规则使用的默认get参数.当使用该规则来解析一个请求时,这个参数的值会被注入到$_GET参数中. matchValue  当创建一

yii2 url 美化参数

'urlManager' => [ 'class' => 'yii\web\UrlManager', 'enablePrettyUrl' => true, 'showScriptName' => false, // 'suffix' => '.html', 'rules'=>[ '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/&l

Yii2.0 URL美化

1. 程序初始化注册文件,加入如下: 'urlManager' =>[ 'class' => 'yii\web\UrlManager', 'showScriptName' =>false, 'enablePrettyUrl' => true, ] 2. 入口同级目录增加 .htaccess,内容如下 RewriteEngine on# If a directory or a file exists, use it directlyRewriteCond %{REQUEST_FILE

Yii2 之 UrlManager 实践 (一)

1.  enablePrettyUrl yii2默认不支持类似 http://<domain>/site/error 的url格式,需要在config.php中启用 enablePrettyUrl 属性 [ //others 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, ], ], ]; 2. 配置suffix 实现伪静态 *.html 需要在 config.php中配置 urlManager 即

关于Yii2.0的url路径优化问题(配置虚拟路径)

backend/config/main.php 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'enableStrictParsing' => false, 'suffix' => '.html', 'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view', ],

yii2.0 输出url 注册js css文件

//输出url <a href="<?=  Url::to(['/users/login/login','id'=>5,'mark'=>true]) ?>" >登录 </a> 以上等同于 <a href="/users/login/login?id=5&mark=true" >登录 </a> //注册css ①css代码 <?php $css=<<<CSS .ti

Yii2 中国省市区三级联动

1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region": "dev-master" 切换到项目目录 composer update; 3.配置 1) 在地区的Model中添加以下方法 public static function getRegion($parentId=0) { $result = static::find()->

我 &amp;&amp; yii2 (路由优化)

今天配置了一下yii2 的路由,用 /index.php?r=... 这样的路由,实在是不太习惯,所以我便试着把yii2 的路由,写成laravel 那般,以下为详情 1.环境介绍 lnmp php5.6, mysql5.5, lnmp1.2 yii2-advanced 2.在 frontend/config/main.php 中,添加以下内容 'urlManager' => [ 'enablePrettyUrl' => true, //开启URL美化  'showScriptName' =&