[Solved] Yii 2 Module Theme

Hello,

reading this page about modules in Yii 2 I’ve found that you can define a specific layout for your module:

This is working fine but I was wondering if it would also be possible to tell the module to use a different theme, for example for an "admin" module.

Of course a module should be self-contained but if you’re able to specify a theme using the module configuration / when setting up the module, that should be fine, shouldn’t it?

Thanks.

Try configuring a view for a module:


$this->components['view'] = [

    'class' => '\yii\web\View',

    'theme' => 'xxx',

];

Hello,

thanks samdark for your help!

I’ve managed to do it using the init method of the module, configuring the properties of the view->theme component:




public function init()

{

    parent::init();


    // custom initialization code:

    \Yii::$app->view->theme->pathMap = ['@app/views' => '@app/themes/admin/views'];

    \Yii::$app->view->theme->baseUrl = '@web/themes/admin';

}



Thanks.

Also, if you are not using a theme for your application (if you did not set the theme component in your config file) you can initialise the component from the init method of the module in this way:




public function init()

{

    parent::init();


    \Yii::$app->view->theme = new \yii\base\Theme([

        'pathMap' => ['@app/views' => '@app/themes/admin/views'],

        'baseUrl' => '@web/themes/admin',

    ]);

}



bye