Yii2中的ForbiddenHttpException问题

时间:2019-12-25 10:46:26

标签: yii2

我尝试在控制器中执行此代码

throw new ForbiddenHttpException();

我很高兴看到403错误,但看到以下错误:

An Error occurred while handling another error:
yii\web\ForbiddenHttpException: You are not allowed to perform this action. in C:\xampp\htdocs\university\vendor\yiisoft\yii2\filters\AccessControl.php:158
Stack trace:
#0 C:\xampp\htdocs\university\vendor\yiisoft\yii2\filters\AccessControl.php(140): yii\filters\AccessControl->denyAccess(Object(yii\web\User))
#1 C:\xampp\htdocs\university\vendor\yiisoft\yii2\base\ActionFilter.php(77): yii\filters\AccessControl->beforeAction(Object(yii\web\ErrorAction))
#2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#3 C:\xampp\htdocs\university\vendor\yiisoft\yii2\base\Component.php(627): call_user_func(Array, Object(yii\base\ActionEvent))
#4 C:\xampp\htdocs\university\vendor\yiisoft\yii2\base\Controller.php(276): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#5 C:\xampp\htdocs\university\vendor\yiisoft\yii2\web\Controller.php(164): yii\base\Controller->beforeAction(Object(yii\web\ErrorAction))
#6 C:\xampp\htdocs\university\vendor\yiisoft\yii2\base\Controller.php(155): yii\web\Controller->beforeAction(Object(yii\web\ErrorAction))
#7 C:\xampp\htdocs\university\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('error', Array)
#8 C:\xampp\htdocs\university\vendor\yiisoft\yii2\web\ErrorHandler.php(108): yii\base\Module->runAction('site/error')
#9 C:\xampp\htdocs\university\vendor\yiisoft\yii2\base\ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\ForbiddenHttpException))
#10 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\ForbiddenHttpException))
#11 {main}

1 个答案:

答案 0 :(得分:1)

我认为您忘记在访问规则中添加错误操作规则 您可以在行为中使用此代码:

  [
       'actions' => ['error'],
       'allow' => true,
       'roles' => ["?","@"],
  ],

或在访问规则中,将only用于需要许可的操作