petiar
petiar

Reputation: 1087

How to set up layout for all controllers in Yii module?

in my Yii app I have one module called admin, so the module class is AdminModule, it extends the CWebModule class and is located in the AdminModule.php file. According the documentation the CWebModule has a layout property which is shared among all module controllers in case the controllers itself do not have any layout defined.

My controller does not have any layout defined and in AdminModule.php i put this:

$this->layout='webroot.themes.bootstrap.views.layouts.column2';
$this->layoutPath = Yii::getPathOfAlias('webroot.themes.bootstrap.views.layouts');

However, my controllers in admin module are still using some other layout, i think it is the one defined in the Controller.php in components directory. Why is that? How do I setup shared layout for a particular module?

Upvotes: 2

Views: 5410

Answers (5)

Majid Abdolhosseini
Majid Abdolhosseini

Reputation: 2301

you can put the following code in your controller.

public function init()
{
    Yii::$app->setLayoutPath($this->module->getBasePath().'/views/layout');
}

or you can put the following code in your module bootstrap class

public function init()
{
    parent::init();

    Yii::$app->setLayoutPath($this->getBasePath().'/views/layout');
}

Upvotes: 0

aXen
aXen

Reputation: 1

if you use Module and Theme(like bootstrap) at the same time,it means Yii will find view file(include layout file, a special view file) in Theme folder firstly,there is an important function named resolveViewFile in CController, you can add debug point on this to watch out how it works, and below is my solution:

1. I have a module named "admin".

2. In AdminModule's init function, add:

$this->layout = 'column2'; 

3. remove module's all view files to theme folder

Upvotes: 0

petiar
petiar

Reputation: 1087

The solution is to slightly change my code, like this:

$this->layoutPath = Yii::getPathOfAlias('webroot.themes.bootstrap.views.layouts');
$this->layout = 'column2';

as with the path specified I do not need to specify whole path alias for a layout. I have these to lines in init() function of my AdminModule.php and it works fine.

Upvotes: 5

Ninad
Ninad

Reputation: 1871

try setting the layout path as shown below

$this->layout="webroot/themes/bootstrap/views/layouts/column2";

Upvotes: 1

Telvin Nguyen
Telvin Nguyen

Reputation: 3559

The solution is set the layout on beforeControllerAction in your module. It should work. I've answer the similar question, please refer to it Yii module layout

Upvotes: 0

Related Questions