Maulana Yusuf Maulana Yusuf - 4 months ago 73
PHP Question

Set layout from module controller in yii2

I have three layout in my layouts folder on main views folder.
I have made a module called "subDomain", on my subDomain module i have a Controller called HomeController. In HomeController there is an action called getDomain.

In getDomain action, i want to change the main layout to getDomainLayout. But it is error when i use this code $this->layout="getDomainLayout", yii2 say

Invalid Parameter – yii\base\InvalidParamException

The view file does not exist: E:\server9\htdocs\myyii2\modules\subDomain\views\layouts../../..//layouts/bersih.php

Answer

You need to put a layout into your module folder:

/modules/moduleName/views/layouts/layoutName.php

Then, in your module controller action you can write:

$this->layout = "layoutName";

Or, you can specify a different path to the layout in your module controller action:

// layout from /frontend/views/layouts/layoutName.php
$this->layout = "@app/views/layouts/layoutName";
Comments