Home  >  Article  >  Backend Development  >  yii2 reports an error when verifying permissions in the controller

yii2 reports an error when verifying permissions in the controller

WBOY
WBOYOriginal
2016-12-01 00:56:591240browse

rbac permissions can be verified normally, but when verifying in the controller, an exception is thrown
The code in the controller is like this

public function beforeAction($action) {

<code>$controller = Yii::$app->controller->id;        //获取控制器
$action = Yii::$app->controller->action->id;        //获取方法
if(Yii::$app->user->can($controller.'_'.$action)){
    return true;
}else{
    throw new \yii\web\UnauthorizedHttpException('你没有操作权限');
}</code>

}
When you get to the page, it will show the prompt that you have no operation permission. The corresponding $controller.'_'.$action data has been added to the database...

Reply content:

rbac permissions can be verified normally, but when verifying in the controller, an exception is thrown
The code in the controller is like this

public function beforeAction($action) {

<code>$controller = Yii::$app->controller->id;        //获取控制器
$action = Yii::$app->controller->action->id;        //获取方法
if(Yii::$app->user->can($controller.'_'.$action)){
    return true;
}else{
    throw new \yii\web\UnauthorizedHttpException('你没有操作权限');
}</code>

}
When you get to the page, it will show the prompt that you have no operation permission. The corresponding $controller.'_'.$action data has been added to the database...

Write the real address in the data, such as:
'/'.$controller.'/'.$action

Please output $controller.'_'.$action This variable, I use it myself under the parent class,
Yii::$app->controller->id and Yii::$app-> ;controller->action->id is null

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn