yii ~ urlManager rules example

之前做產品頁面我的網址都是透過apache設定成

product/xbox360

或是

product/ps3

作法是

RewriteRule ^product/([a-zA-Z0-9_-]+)/?$ product.php?urlName=$1 [L]


現在用yii

如果網址打product一定會是連到Product這個Controller的actionIndex

就是product/index

如果要加參數就是

product/index/id/12345678

這樣就跟我想像的不一樣.........因為照這規則網址勢必會變類似product/index/id/12345678

這時候就要去config裡改urlManager的rules


我這邊rules是新增這段

'product/<id>'=>'product/index',


ProductController的actionIndex

public function actionIndex() {
$id = Yii::app() -> getRequest() -> getQuery('id');
//do something
$this -> render('index');
}



這樣網址打

product/1234567

她就自動變成product/index/id/12345


reference
http://stackoverflow.com/questions/2760869/yii-framework-controller-action-url-parameters

沒有留言: