Set the layout file for error action dynamically

后端 未结 2 1685
野性不改
野性不改 2021-01-24 20:09

I know there are different ways to choose layout file. It can be done

  • in the configuration
  • with Yii::$app->layout = \'...\'
  • with
2条回答
  •  被撕碎了的回忆
    2021-01-24 20:56

    One way could be to set the the layout file for the application in beforeAction in each controller:

    public function beforeAction($action) {
        Yii::$app->layout = $this->layout;
        return parent::beforeAction($action);
    }
    

    It could be optimized by putting this in an extra controller from which all other controllers inherit.

    But I don't really like this solution. Seems to me like a workaround. Also this does not work if the SiteController defines a layout by itself (as $layout property or within a method).

提交回复
热议问题