How to set layout for errorhandler dynamically without module

吃可爱长大的小学妹 提交于 2019-12-11 02:19:10

问题


I am new in Yii2,

When I started creating a website, i found that you can set ErrorAction in configuration like this

 'errorHandler' => [
            'errorAction' => 'site/error',

        ],

That error using layout from layout/main.php. That layout was used when guest visit the page that located in 'view/site'. But when the user log in the view page locate in different folder which is 'view/band' the layout become totally different and using 'layout/BandLayout'. I know that you can change layout dynamically in controller like i did in BandController

public $layout ='BandLayout';

That will change the entire layout in 'view/band'. But when there is an error like '404' the layout still using layout from layout/main.php. I have done some searching but the solution using init() in module. Since i have not learn module, how do I set layout for error layout in controller?

Thank you


回答1:


You should change layout in SiteController, you could use beforeAction, e.g. :

public function beforeAction($action)
{
    if (parent::beforeAction($action)) {
        // change layout for error action
        if ($action->id=='error')
             $this->layout ='BandLayout';
        return true;
    } else {
        return false;
    }
}


来源:https://stackoverflow.com/questions/27573826/how-to-set-layout-for-errorhandler-dynamically-without-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!