Change module layout

I created a module using Gii, called admin, which I want to use an entirely different layout to the rest of the site.

Now I can’t seem to be able to change the default controller from anything but the ‘protected/views/layouts/main.php’ default. I’ve tried setting the $layout and $layoutPath properties in maybe 20 different ways, and still nothing works. I’ve tried changing it in my AdminModule.php class in both the init() function and declaring "public $layout = ". I’ve tried using Yii::app()->layout and Yii::app()->setLayoutPath() directly, I’ve tried setting it in the config file, and I’ve tried changing the layout in the controller using these methods above. Still, my site will load nothing other than the default ‘main’ view.

This is despite the fact that I’m still getting errors when I attempt to specify an invalid layout path, so it’s obviously parsing my input but not using it.

I’ve searched the forums thoroughly and wasn’t able to find a solution to this problem. Can someone throw me a bone? This is driving me nuts!

in your AdminModule.php you can change the layout




public function init()

{

    // import the module-level models and components

    $this->setImport(array(

        'administrator.models.*',

        'administrator.components.*',

    ));


    $this->layout = 'admin';

}



make sure your layoutFile ‘admin.php’ is located in modules/admin/views/layouts

at least this works with v1.0.12

Thanks for your reply, but this is my code:




    public function init()

    {

        // 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.*',

        ));

		

        $this->layout = 'admin';


    }

It’s still showing the protected/views/layouts/main.php layout, I do have an admin.php layout in modules/admin/views/layouts.

To clarify, I’m using yii-1.1.3.r2247

have a look at http://www.yiiframework.com/forum/index.php?/topic/10435-can-a-module-have-its-own-confg-file/page__view__findpost__p__51955

try removing $layout in Controller.php under components.

Thanks! That did the trick! :D I thought I was going crazy :(

A stupid point, but who knows. You need add first on top class:


public $layout;

. Then use:


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

1 Like

thanks, that worked for me.

could you please explain it why?