我有一个网址,看起来像这样:Yii框架urlManager重写规则
<controller>/<action>/param/value
,我希望它喜欢这样的:
param/value
怎样才能实现?
我试过这个规则,但不知道是否可以(控制器是账户,操作是索引)。
'user/<user:.*>' => 'account/index/user/test'
我有一个网址,看起来像这样:Yii框架urlManager重写规则
<controller>/<action>/param/value
,我希望它喜欢这样的:
param/value
怎样才能实现?
我试过这个规则,但不知道是否可以(控制器是账户,操作是索引)。
'user/<user:.*>' => 'account/index/user/test'
如果我正确uderstand你的问题,你想处理的URL是这样的:
mysite.domain/user/username123
并调用的actionIndex在的AccountController与PARAM 用户,这(在这种情况下)等于“username123”
在这种情况下,您可以尝试规则bel流量:
'user/<user:.*>' => 'account/index/<user>'
但也许你会需要更改声明,如果你的动作:
function actionIndex($user){
// code
}
我会避免把PARAMS付诸行动签名,警予好好尝试去用不匹配的签名[处理动作优雅地] ...事实上,将$ user放入将绑定该操作总是需要指定一个$ user,如果您决定更改功能,那么追踪为什么您的操作未被调用将比确定为什么你的$ _GET没有设置...我建议,而不是将$用户添加到签名中,只需在你的acti中执行以下操作上。
//will always run on /user/<USER:.*>
function actionIndex(){
$user = isset($_GET['user'])?$_GET['user']:NULL;
if(!is_null($user)){
//your user specific account action..
}else{
//handle your error gracefully..
}
}
这种方法可以让您的动作更通用。该URL规则应该如下:
'user/<user:.*>' => 'account/index/user/<user>' //user is defined as a get...
希望帮助& &编码快乐!
'urlManager'反过来工作,你需要的是'.htaccess'重定向。 – adamors