Errors to be shown inside the 'layout' of where it has been raised?

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 fully different 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?

According to what qiangxue explained in

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->errorHand‌​ler and assetManager and make your module bootstrappable:…

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:

So I wouldn’t unless you have to. I would first try to load a custom configuration for your module. Please read here:


public function init()



    // initialize the module with the configuration loaded from config.php

    \Yii::configure($this, require(__DIR__ . '/config.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.

Thank you Wade,

your explanations were awesome, I think having an in depth understanding of application bootstrapping process can avoid many upcoming confusions.

Finally got it working with


inside the Module’s Init(). My previous tries things weren’t working as I expected.