2

I am using Yii framework in my web project. now, I have several modules and I want to use only one layout for all modules. I have used following codes for determining the layout for every controller/action in each module:

$this->layoutPath = Yii::getPathOfAlias('application.views.layouts');
$this->layout = '//layouts/myLayout';

Is there any other solution to do this by using same code in init() function of each module? in other word, I have to write the above 2-line code in each action and i think it's not good and i want to reduce my number of lines of codes. for example as follows:

class StaffModule extends CWebModule
{
    public $layout;
    public $layoutPath;
    public function init()
    {
        $this->layoutPath = Yii::getPathOfAlias('application.views.layouts');
                $this->layout = '//layouts/myLayout';
        $this->setImport(array(
            'staff.models.*',
            'staff.components.*',
        ));
    }
}

but it doesn't work. Help me please.

3 Answers3

2

Just use

$this->layout='//layouts/myLayout';

without

$this->layoutPath = Yii::getPathOfAlias('application.views.layouts');

because // mean you specific absolute path from root

Chung
  • 947
  • 1
  • 12
  • 22
0

The approach you are having in init function is in the right direction..I think the problem could be.. as you are defining layoutPath you shouldn't have //layouts..

$this->layoutPath = Yii::getPathOfAlias('application.views.layouts');
$this->layout = 'myLayout';

and you don't need these:

public $layout;
public $layoutPath;
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
0

I've answered similar question on

using common layout for several modules

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

Community
  • 1
  • 1
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39