test.com/nvku想生成test.com/nvku/
- 'urlSuffix'=>'/',
要更改URL格式,我們應該配置urlManager應用元件,以便createUrl可以自動切換到新格式和應用程序可以正確理解新的網址:
- 'urlManager'=>array(
- 'urlFormat'=>'path',
- 'showScriptName'=>false,
- 'urlSuffix'=>'.html',
- 'rules'=>array(
- 'posts'=>'post/list',
- 'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
- 'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),
- ),
- ),
- 'posts'=>'post/list',
- echo $this->createAbsoluteUrl('post/list');
1 輸出http://localhost/yii_lab/index.php/post/view
- 'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
- echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123'));
2輸出http://localhost/yii_lab/index.php/post/998.html?name=123
- 'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),
- echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));
3,輸出http://localhost/yii_lab/index.php/post/998/tody.xml
- 'http://<user:\w+>.yiiblog.info/<_c:(look|seek)>'=>array('<_c>/host','urlSuffix'=>'.boylee'),
- echo $this->createAbsoluteUrl('look/host',array('user'=>'BoyLee', 'mid'=>'ny-001'));
- echo '';
- 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
- RewriteEngine on
- # 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.開啓rewrite
簡單的說,在main.php中簡單設置urlManager,然後講了3條規則,基本都覆蓋到了。最後是隱藏index.php,請記住.htaccess位於index.php同級目錄 ,而不是protected/目錄。其他就簡單了。