Yii2的urlManager URL美化

本文介绍了如何在Yii2中配置URL Manager实现URL美化,并对比了与Yii1.x的区别。通过Nginx重写规则和web.php文件配置,可以将带有查询字符串的URL转换为更加简洁友好的路径形式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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模式了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值