yii2 reports an error when verifying permissions in the controller

WBOY
Release: 2023-03-02 08:16:02
Original
1247 people have browsed it

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>
Copy after login
Copy after login

}
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>
Copy after login
Copy after login

}
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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!