加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Yii2,抛出NotFoundException时发生错误

发布时间:2020-12-13 13:19:12 所属栏目:PHP教程 来源:网络整理
导读:我有一个在我的应用程序的beforeAction事件上运行的简单代码: 'on beforeAction' = function ($event) { throw new yiiwebNotFoundHttpException('The requested page does not exist.');}, 我希望它只显示我的应用程序的404页面,但它会抛出以下错误: A
我有一个在我的应用程序的beforeAction事件上运行的简单代码:
'on beforeAction' => function ($event) {
    throw new yiiwebNotFoundHttpException('The requested page does not exist.');
},

我希望它只显示我的应用程序的404页面,但它会抛出以下错误:

An Error occurred while handling another error:
exception 'yiiwebNotFoundHttpException' with message 'The requested page does not exist.' in /home/files/www/ucms/config/frontend/config.php:9
Stack trace:
0 [internal function]: {closure}(Object(yiibaseActionEvent))
1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Object(Closure),Object(yiibaseActionEvent))
2 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(607): yiibaseComponent->trigger('beforeAction',Object(yiibaseActionEvent))
3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.php(139): yiibaseModule->beforeAction(Object(yiiwebErrorAction))
4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(455): yiibaseController->runAction('error',Array)
5 /home/files/www/ucms/vendor/yiisoft/yii2/web/ErrorHandler.php(85): yiibaseModule->runAction('site/error')
6 /home/files/www/ucms/vendor/yiisoft/yii2/base/ErrorHandler.php(109): yiiwebErrorHandler->renderException(Object(yiiwebNotFoundHttpException))
7 [internal function]: yiibaseErrorHandler->handleException(Object(yiiwebNotFoundHttpException))
8 {main}
Previous exception:
exception 'yiiwebNotFoundHttpException' with message 'The requested page does not exist.' in /home/files/www/ucms/config/frontend/config.php:9
Stack trace:
0 [internal function]: {closure}(Object(yiibaseActionEvent))
1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Object(Closure),Object(yiibaseActionEvent))
3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.php(139): yiibaseModule->beforeAction(Object(yiibaseInlineAction))
4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(455): yiibaseController->runAction('',Array)
5 /home/files/www/ucms/vendor/yiisoft/yii2/web/Application.php(84): yiibaseModule->runAction('',Array)
6 /home/files/www/ucms/vendor/yiisoft/yii2/base/Application.php(375): yiiwebApplication->handleRequest(Object(yiiwebRequest))
7 /home/files/www/ucms/web/index.php(17): yiibaseApplication->run()
8 {main}
85 line上ErrorHandler.php文件中的问题:
$result = Yii::$app->runAction($this->errorAction);

当ErrorHandler尝试运行ErrorAction时,NotFoundHttpException再次被触发,而ErrorHandler只显示没有渲染的错误消息.

解:

public function beforeAction($action)
{
    if(!$action instanceof yiiwebErrorAction) {
        throw new yiiwebNotFoundHttpException('The requested page does not exist.');
    }

    return parent::beforeAction($action);
}

上一个答案:
在生产服务器上还需要在index.php文件中设置正确的环境设置:

defined('YII_DEBUG') or define('YII_DEBUG',false);
defined('YII_ENV') or define('YII_ENV','prod');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读