首先,您必须为“非 Category”操作声明所有规则,然后是动态规则(与 category 和 antry 相关联):
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
// for example if your account and register actions in user controller
// ... you can write
'account' => 'user/account',
'register' => 'user/register',
// or with one rule
'<action(account|register)>' => 'user/<action>',
// and for all other 'static actions', such as login, logout ...
// after yhat you can declire dynamic rules
'<categoryName:\w+>' => 'category/index',
'<categoryName:\w+>/<entryName:\w+>' => 'category/entry'
),
),
所以代码Yii::app()->createUrl('user/register') 将生成url website.com/register,相应地,url website.com/register "goes to" user 控制器的register 动作(所有其他静态像这样的规则)。
现在动态规则:代码
Yii::app()->createUrl('category/index', array(
'categoryName' => 'first-category-name'
))
会生成url website.com/first-category-name,反之亦然:url website.com/first-category- name "goes to" category/index 动作,其中将有 $_GET['categoryName'] 参数,这将是相等的到“第二类名称”․
相应代码
Yii::app()->createUrl('category/index', array(
'categoryName' => 'some-category-name',
'entryName' => 'some-entry-name'
))
会生成url website.com/some-category-name/some-entry-name,在category/entry动作中可以得到$_GET ['categoryName'] 等于 "some-category-name" 和 $_GET['entryName'] 等于 some-entry-name 。
我希望这能帮助你理解 Yii 中的规则是如何工作的。
谢谢!