1

I am trying to get work layout in a module. So I created a layout in view folder of module called 'adminLayout'

Assuming a layout in AdminModule.php in init() method. So now it looks like this:

public function init()
{

    $this->layoutPath = Yii::getPathOfAlias('application.modules.admin.views.layouts');
    $this->layout = 'adminLayout';
    // this method is called when the module is being created
    // you may place code here to customize the module or the application

    // import the module-level models and components
    $this->setImport(array(
        'admin.models.*',
        'admin.components.*',
    ));


}

But for some reason layout didn't apply to module. I tried to add "public $layout" to controller and it works.

Can't figure out what problem is.

Also I tried to add layout settings to main.php in config folder, but still no action. Will be grateful if someone could help.

Mohit Bhansali
  • 1,725
  • 4
  • 20
  • 43
timofeiMih
  • 109
  • 1
  • 9

3 Answers3

4

The solution is set the layout on beforeControllerAction in your module. It should work.

 public function beforeControllerAction($controller, $action)
  {
    if(parent::beforeControllerAction($controller, $action))
    {
      $controller->layout = 'adminLayout';
      return true;
    }
    else
      return false;
  }
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39
2

There are many posts on this subject and the answer is in the Yii docs:

layout property

public mixed $layout;

the layout that is shared by the controllers inside this module. If a controller has explicitly declared its own layout, this property will be ignored. If this is null (default), the application's layout or the parent module's layout (if available) will be used. If this is false, then no layout will be used.

Simply detect the module from within your controller and set the layout accordingly:

class Controller extends CController
{

public function init(){

    //Set layout
    $this->layout = ($this->module->id=='admin') ? '//layouts/column2' : '//layouts/column1';
.........
}
Termine
  • 77
  • 7
0

Create assets folder in your module. Add the following code for assetsURL:

private $_assetsUrl;

public function getAssetsUrl()
{
    if ($this->_assetsUrl === null)
        $this->_assetsUrl = Yii::app()->getAssetManager()->publish(
            Yii::getPathOfAlias('admin.assets') );
    return $this->_assetsUrl;
}

Create a beforeControllerAction function, and add $controller->layout:

public function beforeControllerAction($controller, $action)
{
    if(parent::beforeControllerAction($controller, $action))
    {   
        // this overwrites everything in the controller
        $controller->layout = 'adminLayout';
        // this method is called before any module controller action is performed

        return true;
    }
    else
        return false;
}

Import all your CSS and JS files like:

<link rel="stylesheet" type="text/css" href="<?php echo $this->module->assetsUrl; ?>/css/style.default.css" media="screen, projection" />
Mohit Bhansali
  • 1,725
  • 4
  • 20
  • 43