YII url美化管理

yii的官方文档对此的解释如下:
urlSuffix: 此规则使用的url后缀,默认使用CurlManger::urlSuffix,即为null。例如可以将此设置为.html,让url看起来“像”是一个静态页面。
caseSensitive: 是否大小写敏感,默认使用CUrlManager::caseSensitive,即为null。
defaultParams: 该规则使用的默认get参数。当使用该规则来解析一个请求时,这个参数的值会被注入到$_GET参数中。
matchValue: 当创建一个URL时,GET参数是否匹配相应的子模式。默认使用CurlManager::matchValue,即为null。如果该属性为 false,那么意味着当路由和参数名匹配给定的规则时,将以此来创建一个URL。如果该属性为true,那么给定的参数值夜必须匹配相应的参数子模式。注意:将此属性设置为true会降低性能。

 

test.com/nvku想生成test.com/nvku/

Java代码   收藏代码
  1. 'urlSuffix'=>'/',  

要更改URL格式,我们应该配置urlManager应用元件,以便createUrl可以自动切换到新格式和应用程序可以正确理解新的网址:

Java代码   收藏代码
  1. 'urlManager' => array(  
  2.     'urlFormat' => 'path',  
  3.     'showScriptName' => false//隐藏index.php    
  4.     'urlSuffix' => '.html'//后缀    
  5.     'caseSensitive' => true//设置对大小写不敏感  
  6.     'rules' => array(  
  7.         '<controller:\w+>/<id:\d+>' => '<controller>/view',  
  8.         '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',  
  9.         '<controller:\w+>/<action:\w+>' => '<controller>/<action>',  
  10.     ),  
  11. ),  

规则

Rule代码   收藏代码
  1. 'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),  
Action代码   收藏代码
  1. echo $this->createAbsoluteUrl('post/show',array('id'=>998'name'=>'123'));  

 输出http://localhost/yii_lab/index.php/post/998.html?name=123

 

 规则

Rule代码   收藏代码
  1. 'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),  
Action代码   收藏代码
  1. echo $this->createAbsoluteUrl('post/view',array('id'=>998'mid'=>'tody'));  

 输出http://localhost/yii_lab/index.php/post/998/tody.xml

  规则

Rule代码   收藏代码
  1. 'http://<user:\w+>.yiiblog.info/<_c:(look|seek)>'=>array('<_c>/host','urlSuffix'=>'.boylee'),  

 

Action代码   收藏代码
  1. echo $this->createAbsoluteUrl('look/host',array('user'=>'BoyLee''mid'=>'ny-001'));  
  2. echo $this->createAbsoluteUrl('looks/host',array('user'=>'BoyLee''mid'=>'ny-001'));  

输出 http://BoyLee.yiiblog.info/look.boylee?mid=ny-001

http://localhost/yii_lab/index.php/looks/host/user/BoyLee/mid/ny-001

 

 'posts'=>'post/list',

'sayhello/<name>' => 'post/hello',  name是PostController actionHello($name)的参数
'post/<alias:[-a-z]+>' => 'post/view',   domain/post/e文小写 其中:前面的alias是PostController actionView($alias)的参数
'(posts|archive)/<order:(DESC|ASC)>' => 'post/index',  domain/posts/DESC或domain/posts/ASC
'(posts|archive)' => 'post/index',  domain/posts或domain/archive

'tos' => array('website/page', 'defaultParams' => array('alias' =>'terms_of_service')),
When the URL is /tos, pass terms_of_service as the alias parameter value.

 

隐藏 index.php

还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿index.php 入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。

1.add showScriptName=>false

2.add project/.htaccess

Java代码   收藏代码
  1. RewriteEngine on  
  2.   
  3. if a directory or a file exists, use it directly  
  4. RewriteCond %{REQUEST_FILENAME} !-f  
  5. RewriteCond %{REQUEST_FILENAME} !-d  
  6.   
  7. # otherwise forward it to index.php  
  8. RewriteRule . index.php  

3.开启rewrite

简单的说,在main.php中简单设置urlManager,然后讲了3条规则,基本都覆盖到了。最后是隐藏index.php,请记住.htaccess位于index.php同级目录 ,而不是protected/目录。其他就简单了。

yii隐藏index.php不隐藏admin.php

安装那个办法确实把前台中的index.php隐藏掉了,但同时也隐藏掉了admin.php,于是进入后台的时候URL就乱套了。路径都有问题。解决办法就是:在protected/admin/config/main.php文件中加入下面一行代码:'showScriptName' => true, 'urlSuffix'=>'/'

 

开启url美化后bug:“项目protect绝对路径/xxxxx/xxx.html”我的解决方法是在components/Controller.php中修改

Java代码   收藏代码
  1. public function init(){  
  2.     Yii::app()->getRequest()->setBaseUrl(Yii::app()->request->hostInfo);  
  3. }  

YII模块绑定二级域名的方法

将所有子域名都转化为模块的路由规则

Java代码   收藏代码
  1. 'http://<_m:\w+>.xxx.com<_q:.*>/*'=>'<_m><_q>',    

 规则写在main.php中的组件配置数组里面:

Java代码   收藏代码
  1. 'urlManager' => array(  
  2.     'urlFormat' => 'path',  
  3.     'showScriptName' => false//隐藏index.php  
  4.     'rules' => array(  
  5.         'http://<_m:\w+>.xxx.com<_q:.*>/*'=>'<_m><_q>',  
  6.     ),  
  7. ),  

 如果仅仅是一个模块(假设是user)需要绑定子域名,可以这样写规则:

Java代码   收藏代码
  1. 'http://user.xxx.com<_q:.*>/*'=>'user<_q>',   

 如果是若干模块需要绑定子域名,可以这样写:

Java代码   收藏代码
  1. 'http://<_m:(user|admin|photo)>.xxx.com<_q:.*>/*'=>'<_m><_q>',   

在使用的时候我注意到,如果是www开头的网址也会被转向到www模块,那么你可以使用(A|B|C)这样的规则选择要启用的模块,或者使用正则来滤掉www。

另外在路由规则里面,如果某一条匹配,则不会向下进行匹配。这是需要注意的,但是正是这样,路由才更高效,更灵活。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值