我需要为以下url架构执行路由:
website.com/some-category-name
website.com/some-category-name/entryName某些类别名称将是可变的-某个类别的名称
如何为其配置路由?我需要输入以前的控制器,例如:
website.com/account
website.com/regiter并希望将所有没有控制器名称(因此将是类别名称)的内容转到控制器类别。
我算不出来。
发布于 2015-07-03 19:13:23
使用
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'categoryName/<categoryName:\w+>' => array('site/category'),
'register' => array('site/register'),
'account' => array('site/account')
),
),发布于 2015-07-03 21:03:58
首先,您必须为“非类别”操作声明所有规则,然后声明动态规则(与类别和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将“转到”用户控制器的注册操作(所有其他类似的静态规则)。
现在动态规则:代码
Yii::app()->createUrl('category/index', array(
'categoryName' => 'first-category-name'
)) 将生成url website.com/__first-category-name,,反之亦然: url website.com/__first-category-name“to to”category/index操作,其中将有可用的$_GET'categoryName'参数,该参数将等于"second-category-name"․。
相应的代码
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的规则是如何工作的。
谢谢!
https://stackoverflow.com/questions/31203607
复制相似问题