Changing layout in Admin Module

Hi all,

I’ve set up an Admin Module. Problem I have is to set up new layout for this Admin part.

Whatever I do, Yii uses my protected/views/layout

I write


$this->layout = '$this->layout ='//layouts/column2-admin';

in AdminModule init() with column2-admin located in modules/admin/views/layouts

But nothing…I stay with the layout of the classic web app.

Do you no if there is an another way of doing ?

Thanks

Check this documentation.

I think that you have to use a single slash at the beginning:




/layouts/column2-admin



Thanks zaccaria,

but does not work.

Something that I wonder…

In module I do not have a Controller class in Components whereas in protected/components I have One which defines the layout of all Controllers to


public $layout='//layouts/column1';

Then, I think that when I request an Admin action,

-1- AdminModule (child of CWebModule) is called. In this we set the targeted layout


$this->layout='/layouts/column2-admin';

-2- After that I suppose that (In my case) DefaultController of Admi module is called. But by doing this I use the Controller Class of protected/components which defines another layout …

Then How does it stand. For my point of view defining something in AdminModule (child of CWebModule) is useless

I’m a bit lost in the dust …

I think if your controllers extend from ‘Controller’ then it will use the layout defined in there. (protected/components/Controller.php)

Maybe yes, is useless to define in AdminModule.

As a test, try to set the layout directly in in the action of the module, and check if it works (just for be sure that this call to the layout is correct).

If the problem is your master class, you can always use a new one for admin module.

I precise that when I write


$this->layout = 'column2-admin'

in DefaultController (or in whatever controllers in Admin Module), then http://localhost/webapp/index.php?r=admin/default works fine and I obtain the right layouts located in my modules/admin/views/layouts…

For sure by doing this I override the Controller base class (which sets layout).

But I do not like this solution (I hate this solution). I can’t imagine setting all my Admin’s controllers by this way.

I read a lot of docs that indicates to set up this layout in the AdminModule (child of CWebModule) But for me, Whatever I write in this class (I mean write concerning layout) nothing is taken into account.

I precise also, that I just begin my webapp, and my module, then I think that I’m not so far from the right way of doing the things (because no time for bug introduction :lol: )

The thing that I do for having something cleaner is that I duplicate protected/Components/controller in modules/admin/components

Then I write


public $layout='column2-admin';

(Thanks zaccaria for / instead of //)

By this way all of my Controllers of admin module inherit of this property.

But I persit to wonder why some people says to set this layout in AdminModule (child of CWebModule)

Hey, naranjo here…

I created an admin module and I was wrestling with this same problem for the longest. The module was using the main application layout even though I specified for it to use a different layout in AdminModule(as shown in the Yii book).

These are the steps I did to make it work.

From protected/components, I copied Controller to protected/modules/admin/components.

From protected/views/layouts, I copied column1, column2 and main (I just copied and edited main for the admin module, although you can rename it to whatever as long as you reference it in AdminModule) to protected/modules/admin/views/layouts.

In the init method of AdminModule, I specified the copied main layout to be used.




$this->layout = 'main';



In the copied column1 and column2, I removed the ‘//layouts/main’ from the call to beginContent.




<?php $this->beginContent(); ?>



Finally, in the copied Controller, I specified the layout as column1 (or column2), without //layouts.




public $layout='column1';



Y así funcionó. That’s how I got it to work. Perhaps that will help some of you out there.

Saludos

@naranjo:

thanks, that worked for me, too. However, you can make it a bit easier. You don’t necessarily have to copy the Controller.php. Instead, you can use the following in AdminModule.php:


public function beforeControllerAction($controller, $action)                                                  

        {                                                                                                             

                if(parent::beforeControllerAction($controller, $action))                                              

                {

                        $controller->layout = 'column2';


                        // this method is called before any module controller action is performed                     

                        // you may place customized code here                                                         

                        

                        return true;

                }

                else

                        return false;

        }



found that here: http://www.yiiframework.com/forum/index.php?/topic/10435-can-a-module-have-its-own-confg-file/page__p__51955#entry51955

I have admin module which already set $controller->layout = ‘adminmain’; in AdminModule.php public function beforeControllerAction($controller, $action)

it apply ‘adminmain’ layout to all my admin controllers, however, I want my some of other admin controllers to use layout ‘column2’ for example, how to deal with that?

Hi folks,

this is a bit late reply considering the age of this post. But I have been struggling with the exact same problem and found the following solution, maybe it is useful to someone.

Yii v. 1.1.2 In AdminModule init function

$this->layoutPath = Yii::getPathOfAlias(‘admin.views.layouts’);

$this->layout = ‘main’;

in DefaultController

public $layout = ‘column1’;

or in an action e.g.

$this->layout = ‘column2’;

You have to translate admin to your modulename of course :slight_smile:

Cheers