2011-03-26 22:40:06 0 评论 Yii 1.0 Boy.Lee

Yii 1.0 使用 UrlManager 格式化 URL

{Location}

>> protected/config/main.php

>> components

>> urlManager

 

{Basic Struction}

'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
//some rules here.
),


{Rule No. One}

>Rule
'posts'=>'post/list',

>Action
echo $this->createAbsoluteUrl('post/list');

>Display
http://yiilib.com/yii_lab/index.php/posts

 

{Rules No. Two}

>Rule
'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),

>Action
echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123'));

>Display
http://yiilib.com/yii_lab/index.php/post/998.html?name=123

 

{Rules No.Three}

>Rule
'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),

>Action
echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));

>Display
http://yiilib.com/yii_lab/index.php/post/998/tody.xml

 

{Rules No.Four}

>Rule
'http://<user:\w+>.yiilib.com/<_c:(look|seek)>'=>array('<_c>/host','urlSuffix'=>'.mp4'),

>Action
echo $this->createAbsoluteUrl('look/host',array('user'=>'BoyLee', 'mid'=>'ny-001'));
echo $this->createAbsoluteUrl('looks/host',array('user'=>'BoyLee', 'mid'=>'ny-002'));

>Display
http://BoyLee.yiilib.com/look.mp4?mid=ny-001
http://yiilib.com/looks/host/user/BoyLee/mid/ny-002

 

{Hidden index.php}

1.add showScriptName=>false

'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
2.add ......./yii_lab/.htaccess , be brother with index.php and protected, and add this code in it.

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteBase /yii_lab

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
3.make .htaccess working ....

look at this

4.make sure your rewrite is open

look at this

 

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