3

It's common to have separate layouts in multi-tier applications. Suppose that there are 'admin', 'forum' and 'site' tier or modules and each one has its own layout in views.

The worst thing about YII error handling is that it's not possible to change the layout file for error handler based on application tier or module.

In YIIs global configuration file we enable errorHandler module as following:

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

we define an error handler action globally for the whole application and there is no option the change it during app runtime. Even inside that errorAction there is no option to change the layout file or the view file outside its scope.

What solution could be possible and enough straightforward to have each error to be shown inside the layout of where it has been raised?

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
Rev
  • 304
  • 1
  • 5
  • 11
  • `Yii::$app->errorHandler->errorView = 'views/error.php'` http://www.yiiframework.com/doc-2.0/yii-web-errorhandler.html#$errorView-detail – Muhammad Shahzad Aug 12 '16 at 05:42
  • Dear Muhammad, This changes the view file rendering the error message, not the layout associated with that – Rev Aug 12 '16 at 06:13
  • $this->layout = 'yourNewLayout'; pls try this. http://stackoverflow.com/questions/29982759/change-layout-file-within-a-view-in-yii2 – Muhammad Shahzad Aug 12 '16 at 06:59
  • One question : Why you want error page module / tier wise? – Nana Partykar Aug 12 '16 at 07:58
  • @muhammad-shahzad couldn't make it running. according to YII docs Controller::$layout is: 'The name of the layout to be applied to this controller's views.' not the layout path. – Rev Aug 12 '16 at 09:09
  • @nana-partykar because each module has it's own fully different layout, tabs, menus, etc and want to show the error message inside that context. – Rev Aug 12 '16 at 09:10
  • according to what qiangxue explained in https://github.com/yiisoft/yii2/issues/4484 : Both error handler and asset manager are global. If you want a module to use its own error handler/asset manager, you have to replace Yii::$app->errorHandler and assetManager and make your module bootstrappable: https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-modules.md#bootstrapping-modules- but couldn't figure out how to reach this using module bootstrapping, as what qiangxue explained. – Rev Aug 12 '16 at 09:23
  • Possibly solved with [this trick](http://stackoverflow.com/a/43761325/57091). Have a look, please. – robsch May 03 '17 at 13:28
  • For me it's been solved by setting error handler during runtime. A good practice was to always inherit all controllers from a custom controller rather than directly inheriting from yii\web\Controller. This has solved many issues including this. – Rev May 04 '17 at 15:00

1 Answers1

0

yii2 doc You can specify errorAction in any controller with:

public function actionError(){
    //your code is here
}

public function actions(){
return [
    'error' => ['class' => 'yournamespace\YourAction'],
];
}
ekaratas
  • 92
  • 5
  • Yii2 never triggers `actionError()` inside a controller. You can test it, I've tested it and YII always triggers `actionError()` of what you've specified in config file. – Rev Aug 13 '16 at 00:10
  • `public function actions(){ return [ 'error' => ['class' => 'yournamespace\YourAction'], ]; }` You can, I'm using this. It works – ekaratas Aug 15 '16 at 07:08
  • How do you switch layouts inside YourAction ? – Rev Aug 17 '16 at 04:31
  • Yii always renders error inside the layout of what you define in configuration, no matter what `errorAction` you pass to `errorHandler`. `errorAction` just formats the error message. – Rev Aug 17 '16 at 04:42