php - Yii2,抛出 NotFoundException 时出错

标签 php yii2

我有一个简单的代码在我的应用程序的 beforeAction 事件上运行:

'on beforeAction' => function ($event) {
    throw new \yii\web\NotFoundHttpException('The requested page does not exist.');
},

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

An Error occurred while handling another error:
exception 'yii\web\NotFoundHttpException' 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(yii\base\ActionEvent))
1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Object(Closure), Object(yii\base\ActionEvent))
2 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(607): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.php(139): yii\base\Module->beforeAction(Object(yii\web\ErrorAction))
4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('error', Array)
5 /home/files/www/ucms/vendor/yiisoft/yii2/web/ErrorHandler.php(85): yii\base\Module->runAction('site/error')
6 /home/files/www/ucms/vendor/yiisoft/yii2/base/ErrorHandler.php(109): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
7 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
8 {main}
Previous exception:
exception 'yii\web\NotFoundHttpException' 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(yii\base\ActionEvent))
1 /home/files/www/ucms/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Object(Closure), Object(yii\base\ActionEvent))
2 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(607): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
3 /home/files/www/ucms/vendor/yiisoft/yii2/base/Controller.php(139): yii\base\Module->beforeAction(Object(yii\base\InlineAction))
4 /home/files/www/ucms/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('', Array)
5 /home/files/www/ucms/vendor/yiisoft/yii2/web/Application.php(84): yii\base\Module->runAction('', Array)
6 /home/files/www/ucms/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
7 /home/files/www/ucms/web/index.php(17): yii\base\Application->run()
8 {main}

最佳答案

ErrorHandler.php 文件中的问题 85 line :

$result = Yii::$app->runAction($this->errorAction);

ErrorHandler 尝试运行 ErrorAction 时,NotFoundHttpException 再次触发并且 ErrorHandler 只显示错误消息而不渲染。

解决方法:

public function beforeAction($action)
{
    if(!$action instanceof \yii\web\ErrorAction) {
        throw new \yii\web\NotFoundHttpException('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');

关于php - Yii2,抛出 NotFoundException 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737621/

相关文章:

php - 从 PDOException $e 返回特定项目

php - (P.H.P.MyAdmin 4.5.3.1+10.0.17-MariaDB) 为什么点击选项在我的表上使用不完整的 WHERE 子句执行?

php - 在 Windows 上运行 PHP 应用程序 - 守护进程还是 cron?

javascript - 在 Yii 中进行 onKeyPress 事件的正确方法是什么?

php - yii 规则从数据库验证

php - 创建时出现 Yii2 动态表单错误 - 必须设置 'model' 属性并且必须从 '\yii\base\Model' 扩展

php - 多维数组输出。请帮我提取并遍历这个烂摊子

php - mysql_fetch_array()期望参数1是资源问题

php - 如何在没有控制台访问权限的情况下处理主机上的 yii2 迁移?

mysql - 如何在 Yii2 中制作多个 UPSERT?