Jah*_*mad 1 action default yii
我想更改控制器的默认操作取决于登录的用户.我的网站中有两个用户:发布者和作者,我希望在发布者登录时将发布者操作设置为默认操作,对作者也是如此.
我该怎么办?我何时可以检查我的角色并设置相关的操作?
另一种方法是在控制器的方法中设置defaultAction属性.有点像这样:init()
<?php
class MyAwesomeController extends Controller{ // or extends CController depending on your code
public function init(){
parent::init(); // no need for this call if you don't have anything in your parent init()
if(array_key_exists('RolePublisher', Yii::app()->authManager->getRoles(Yii::app()->user->id)))
$this->defaultAction='publisher'; // name of your action
else if (array_key_exists('RoleAuthor', Yii::app()->authManager->getRoles(Yii::app()->user->id)))
$this->defaultAction='author'; // name of your action
}
// ... rest of your code
}
?>
Run Code Online (Sandbox Code Playgroud)
查看CAuthManagergetRoles(),看看返回的数组是否具有格式'role'=>CAuthItem object,这就是我正在检查的原因array_key_exists().
如果你不知道,动作名称将只是没有动作部分的名称,例如,如果你有public function actionPublisher(){...}动作名称应该是:publisher.