2015-09-23 12 views
7

Tôi có một mã đơn giản đang chạy trên beforeAction trường hợp ứng dụng của tôi:Yii2, lỗi xảy ra trong khi ném NotFoundException

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

tôi hy vọng nó chỉ đơn giản là hiển thị trang 404 của ứng dụng của tôi, nhưng nó ném lỗi sau:

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} 

Trả lời

7

vấn đề ở ErrorHandler.php file trên 85 line:

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

Khi ErrorHandler thử chạy ErrorAction, NotFoundHttpException kích hoạt lại và ErrorHandler chỉ hiển thị thông báo lỗi mà không hiển thị.

Giải pháp:

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); 
} 

trước Trả lời: Trên máy chủ sản xuất cũng cần thiết lập ngay các môi trường thiết lập trong file index.php của bạn:

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

Bạn nói đúng. Tôi sửa câu trả lời của tôi :) –

+1

Tuyệt vời! :) bạn là đúng, nó giống như một đệ quy trước khi sửa chữa này. Cảm ơn! –

Các vấn đề liên quan