Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set layout from module controller in yii2

I have three layouts in my layouts folder in main views folder. I added a module called subDomain. In my subDomain module I have a Controller called HomeController. In HomeController there is an action called getDomain().

In the getDomain() action I want to change the main layout to getDomainLayout. But there is an error when I use a code:

$this->layout = "getDomainLayout";

Yii2 throws:

Invalid Parameter – yii\base\InvalidParamException    
The view file does not exist: \myyii2\modules\subDomain\views\layouts\bersih.php
like image 659
Maulana Yusuf Avatar asked Aug 06 '15 09:08

Maulana Yusuf


People also ask

How to set layout in yii2?

You can add $this->layout = 'main'; in the module init method. The main. php should be located inside the modules view folder under layouts.

What is module in yii2?

Modules are self-contained software units that consist of models, views, controllers, and other supporting components. End users can access the controllers of a module when it is installed in application. For these reasons, modules are often viewed as mini-applications.


1 Answers

You can set the variable in the controller.

class DefaultController extends Controller
{
     public $layout = 'main.php';
}

Or by passing the complete path

public $layout = '@frontend/modules/idModule/views/layouts/main.php';
like image 102
rafaelvaloto Avatar answered Sep 21 '22 08:09

rafaelvaloto