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?
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: github.com/yiisoft/yii2/blob/master/docs/guide/…
but couldn’t figure out how to accomplish this using module bootstrapping, as what qiangxue explained.
Yii is an app. The framework is in your “vendors” directory. So everything you mess with, is the app. You could scrap the entire thing (advanced, basic) and make your own from scratch, simply by creating a new composer.json and requiring the framework (and a few other dependencies). Then build from there, your own custom app. I have talked to some people and they don’t think outside of the “basic” or “advanced”. Basic and Advanced are basically a module. The root top-most module.
A module can load it’s own custom config, just like the main Yii app (ie: /frontend/config/main.php). So each one of your modules (admin, forum, site) can have their own config, specifying their own error handler, so you can tell your “forum” module to use ‘foo/error’ and tell your “admin” module to use ‘bar/error’. Then each of those specify their own layout.
I don’t know if you really need to make it bootstrapable:
public function init()
{
parent::init();
// initialize the module with the configuration loaded from config.php
\Yii::configure($this, require(__DIR__ . '/config.php'));
}
/your/custom/module/config.php
<?php
return [
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
],
// child components of your custom component
],
'params' => [
// your components own params
],
];
I would use the custom config for each module (shown above), so that later you can easily add more things to the config, such as more child modules, special module params, load extensions that only are for the module, etc.
However, if you don’t want to load a special config, then…
You could set it in your Module’s init:
public function init()
{
parent::init(); // must be first
Yii::$app->errorHandler->errorAction = 'foo/default/error';
}
Lastly, you could define the error action as relative, just ‘error’ instead of ‘site/error’. Then have an actionError() in your controller, and specify the layout in the controller.