Yii2,模块错误页面未显示但在错误404时显示空白页面

pup*_*oyo 2 error-handling custom-error-pages yii2

我已经尝试actionError在模块DefaultController中编写自己的,只是为了呈现错误页面(HTML仅用于测试),它也只是显示空白页面而不是显示错误页面.

我在下面做正确的方法吗?仅当我尝试访问模块路径中的不存在页面时,它才显示空白页面.

在我的模块类中,我errorHandlerinit()函数中配置了如下组件:

public function init()   
{   
    parent::init();   
    // initialize the module with the configuration loaded from config.php   
    \Yii::configure($this, require(__DIR__ . '/config.php'));   

    \Yii::$app->setComponents([
        'errorHandler' => [
            'class' => 'yii\web\ErrorHandler',
            'errorAction' => 'studconsult/default/error',
        ] // set error action route - this to be error action in DefaultController
    ]);             
}
Run Code Online (Sandbox Code Playgroud)

在我的DefaultController班上,我的代码如下:

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

soj*_*oju 10

你应该简单地使用这个:

public function init()
{
    parent::init();
    Yii::$app->errorHandler->errorAction = 'studconsult/default/error';
}
Run Code Online (Sandbox Code Playgroud)

以前的答案不太好,因为它会重置组件.