yii url 重新

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

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

要更改URL格式,我們應該配置urlManager應用元件,以便createUrl可以自動切換到新格式和應用程序可以正確理解新的網址:

Java代碼  收藏代碼
  1. 'urlManager'=>array(  
  2.     'urlFormat'=>'path',  
  3.     'showScriptName'=>false,  
  4.     'urlSuffix'=>'.html',  
  5.     'rules'=>array(  
  6.         'posts'=>'post/list',  
  7.         'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),  
  8.         'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),  
  9.     ),  
  10. ),  
Rule代碼  收藏代碼
  1. 'posts'=>'post/list',  

 

Action代碼  收藏代碼
  1. echo $this->createAbsoluteUrl('post/list');  

1 輸出http://localhost/yii_lab/index.php/post/view

 

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

 

Action代碼  收藏代碼
  1. echo $this->createAbsoluteUrl('post/show',array('id'=>998'name'=>'123'));  

 2輸出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'));  

 3,輸出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 '';  
  3. echo $this->createAbsoluteUrl('looks/host',array('user'=>'BoyLee''mid'=>'ny-001'));  

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

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

 

1)controller/Update/id/23
public function actionUpdate(){
$id = Yii::app()->request->getQuery('id') ; 經過處理的$_GET['id']

}

//$id = Yii::app()->request->getPost('id'); 經過處理的$_POST['id']

//$id = Yii::app()->request->getParam('id'); //CHttpRequest更多

2)public function actionUpdate($id)  這種不支持多主鍵,會檢查一下到底GET裏面有沒有id,沒有id就直接不允許訪問

 

'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/目錄。其他就簡單了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章