CUrlManager 讓 URL 更具可讀性與 SEO 友善。本文詳細說明常用屬性、規則範例與隱藏入口腳本步驟。
urlSuffix:URL 後綴(如 .html),預設無。caseSensitive:是否大小寫敏感,預設 true。defaultParams:預設 GET 參數,解析請求時自動注入 $_GET。matchValue:建立 URL 時是否檢查參數值符合子模式,開啟會影響效能。'rules' => array(
'posts' => 'post/list',
'post/' => 'post/read',
'post//' => 'post/read',
)
$this->createUrl('post/list') → /index.php/posts$this->createUrl('post/read', array('id'=>100)) → /index.php/post/100$this->createUrl('post/read', array('year'=>2008,'title'=>'a sample post')) → /index.php/post/2008/a%20sample%20post'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'urlSuffix' => '.html',
'rules' => array(
'posts' => 'post/list',
'post/' => array('post/show', 'urlSuffix' => '.html'),
'post//' => array('post/view', 'urlSuffix' => '.xml'),
),
)
createUrl('post/show', array('id'=>998, 'name'=>'123')) → /post/998.html?name=123createUrl('post/view', array('id'=>998, 'mid'=>'tody')) → /post/998/tody.xmlshowScriptName => false.htaccess:RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
mod_rewrite。public function actionUpdate($id) { ... } // 自動綁定 $_GET['id']
// 或
$id = Yii::app()->request->getQuery('id');
規則過多會降低解析效能,高流量網站應精簡規則數量。