2

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?

petiar
  • 1,047
  • 2
  • 14
  • 31

5 Answers5

5

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.

petiar
  • 1,047
  • 2
  • 14
  • 31
1

try setting the layout path as shown below

$this->layout="webroot/themes/bootstrap/views/layouts/column2";
Ninad
  • 1,871
  • 3
  • 15
  • 23
0

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

Community
  • 1
  • 1
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39
  • Thanks for your response. See my answer - it is doable in init(), I have just done the code wrong. Besides I think it is semantically incorrect to put these sort of stuff into the `beforeControllerAction()`. I am not sure, but I would use this function to specifiy **actions** which has to be performed before a controller gets triggered rather than make some configuration changes. – petiar Aug 02 '13 at 16:38
  • Because I went through some tickets where some people asked about the setting layout did not work on init. If it worked with you, it would be good since it solved your problem. – Telvin Nguyen Aug 02 '13 at 17:28
0

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

aXen
  • 1
0

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');
}
Majid Abdolhosseini
  • 2,191
  • 4
  • 31
  • 59