yii2中的ForbiddenHttpException不返回样式化的错误操作

时间:2017-11-07 18:01:29

标签: php yii

我是yii的新手,我在一些仅用于管理员角色的控制器中有以下代码。

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            //'only' => ['index', 'view', 'create', 'update','delete'],
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['admin'],
                ],
            ],
        ],
    ];
}

根据需要阻止从非管理员角色对此控制器的所有尝试,但不会返回样式化的yii错误操作。 我的main.php文件中有以下组件:

'errorHandler' => [
      'errorAction' => 'main/error',
],

但它没有用,我得到的只是:

An Error occurred while handling another error:
yii\web\ForbiddenHttpException: شما برای انجام این عملیات، دسترسی ندارید. in C:\xampp\htdocs\food\vendor\yiisoft\yii2\filters\AccessControl.php:151
Stack trace:
#0 C:\xampp\htdocs\food\vendor\yiisoft\yii2\filters\AccessControl.php(134): yii\filters\AccessControl->denyAccess(Object(yii\web\User))
#1 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\ActionFilter.php(75): yii\filters\AccessControl->beforeAction(Object(yii\web\ErrorAction))
#2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#3 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Component.php(545): call_user_func(Array, Object(yii\base\ActionEvent))
#4 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Controller.php(272): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#5 C:\xampp\htdocs\food\vendor\yiisoft\yii2\web\Controller.php(164): yii\base\Controller->beforeAction(Object(yii\web\ErrorAction))
#6 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Controller.php(154): yii\web\Controller->beforeAction(Object(yii\web\ErrorAction))
#7 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Module.php(523): yii\base\Controller->runAction('error', Array)
#8 C:\xampp\htdocs\food\vendor\yiisoft\yii2\web\ErrorHandler.php(95): yii\base\Module->runAction('main/error')
#9 C:\xampp\htdocs\food\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}
Previous exception:
yii\web\ForbiddenHttpException: شما برای انجام این عملیات، دسترسی ندارید. in C:\xampp\htdocs\food\vendor\yiisoft\yii2\filters\AccessControl.php:151
Stack trace:
#0 C:\xampp\htdocs\food\vendor\yiisoft\yii2\filters\AccessControl.php(134): yii\filters\AccessControl->denyAccess(Object(yii\web\User))
#1 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\ActionFilter.php(75): yii\filters\AccessControl->beforeAction(Object(yii\base\InlineAction))
#2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#3 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Component.php(545): call_user_func(Array, Object(yii\base\ActionEvent))
#4 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Controller.php(272): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#5 C:\xampp\htdocs\food\vendor\yiisoft\yii2\web\Controller.php(164): yii\base\Controller->beforeAction(Object(yii\base\InlineAction))
#6 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Controller.php(154): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#7 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Module.php(523): yii\base\Controller->runAction('', Array)
#8 C:\xampp\htdocs\food\vendor\yiisoft\yii2\web\Application.php(102): yii\base\Module->runAction('main/', Array)
#9 C:\xampp\htdocs\food\vendor\yiisoft\yii2\base\Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
#10 C:\xampp\htdocs\food\admin\web\index.php(20): yii\base\Application->run()
#11 {main}

任何指导都很高兴。

2 个答案:

答案 0 :(得分:0)

在您的主控制器中,您应该:

public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
    ];
}

答案 1 :(得分:0)

我认为要显示错误页面,我需要将错误操作的权限授予非管理员用户。

[
    'actions' => 
           ['logout', 'reset-password', 'request-password-reset', 'error'],
           'allow' => true,
           'roles' => ['branch'],
],