[solved] How to use a layout in module

hi.

I created module gimnastyka using gii and added /modules/gimnastyka/views/layouts/main.php

inside module. Modukle definition:




namespace app\modules\gimnastyka;


class Module extends \yii\base\Module

{

    public $controllerNamespace = 'app\modules\gimnastyka\controllers';


    public function init()

    {

        parent::init();

		

        // custom initialization code goes here

        $this->layout = 'main';

    }

}

But controllers in module use application`s layout.

How to setup module`s layout ?

1 Like

EDIT:

I just tested your above solution and it works at my module?! …

View Path:

app/modules/myModule/views/layouts/column2.php

In Module.php:




    public function init()

    {

        parent::init();


        $this->layout = 'column2';

    }



========================

Other solutions to archive what you want would be:

You place your layout inside your module:

app/modules/myModule/views/layouts/moduleLayout.php

Then in any controller you want the layout of the module add:




namespace app\modules\myModule\controllers;


use yii\web\Controller;


class SomeController extends Controller

{

	## public $layout='/appLayout'; // use layout from app view folder

	public $layout='moduleLayout'; // use layout from module view folder


    public function actionSomeAction()

    {

        return $this->render('something');

    }

}



If you want to use the layout for the whole module you could create a "BaseController" like:




namespace app\modules\myModule\controllers;


use yii\web\Controller;


class BaseController extends Controller

{

	public $layout='moduleLayout';

}



And change every other controller to inherit from BaseController:




namespace app\modules\myModule\controllers;


use yii\web\Controller;


class SomeOtherController extends BaseController

{

	... ... ...

}



Hope this helps?

Regards

$this->layout in init() works perfectly fine but your problem might be here:


class Gimnastyka extends \yii\base\Module

instead of


class Module extends \yii\base\Module

Try this in your module:




    public function init()

    {

        parent::init();


        $this->setAliases([

            '@moduleName' => __DIR__

        ]);


        $this->setViewPath('@moduleName/views');

    }



And then use this in your controller:




    public $layout = 'main';



thanks for solutions.

it turned that the layout in module works now.

maybe something stayed in the cache.

@MetaCrawler, @janisto your solutions works too.

thanks very much.