Yii Framework Forum: Module Layout - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Module Layout Rate Topic: -----

#1 User is offline   Luke Jurgs 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 162
  • Joined: 16-October 11
  • Location:Australia

Posted 17 October 2011 - 12:34 AM

I created a module called administration and I wanted to use a layout in administration/views/layouts called main. The DefaultController insisted on using the application layout, I noticed when I searched the forums a few other people had the same problem and used a work around where they set the layout in the controller and not in the module as is recommended in the documentation.

The documentation recommends setting module::layout and module::layoutPath and as CController defaults to a NULL layout it will use the module layout.

What I missed however is that the DefaultController generated by gii during module generation actually extends Controller not CCcontroller and if you generated your app from the CLI tool then the layout for Controller defaults to:

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


Hope this saves someone from the stupid mistake I made.

My sollution was to have DefaultController extend CController and do:

$this->layoutPath = Yii::getPathOfAlias('administration.views.layouts');
$this->layout = 'administration';


in AdministrationModule::init()
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 17 October 2011 - 02:18 AM

NOTE: moved to proper section (Tips, Snippets, tutorials instead of General Discussion)

Hi Luke

welcome to the yii forum and thanks for the useful post
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   VladimirV 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 03-October 11

Posted 07 November 2011 - 07:22 AM

View PostLuke Jurgs, on 17 October 2011 - 12:34 AM, said:

I created a module called administration and I wanted to use a layout in administration/views/layouts called main. The DefaultController insisted on using the application layout, I noticed when I searched the forums a few other people had the same problem and used a work around where they set the layout in the controller and not in the module as is recommended in the documentation.

The documentation recommends setting module::layout and module::layoutPath and as CController defaults to a NULL layout it will use the module layout.

What I missed however is that the DefaultController generated by gii during module generation actually extends Controller not CCcontroller and if you generated your app from the CLI tool then the layout for Controller defaults to:

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


Hope this saves someone from the stupid mistake I made.

My sollution was to have DefaultController extend CController and do:

$this->layoutPath = Yii::getPathOfAlias('administration.views.layouts');
$this->layout = 'administration';


in AdministrationModule::init()


In case you're using the module generated by gii, you'll also need to do the following:
1. Copy the files from the main 'layouts' folder into your module's layouts folder (it is empty);
2. Remove from your module's views/default/index.php :
<?php
$this->breadcrumbs=array(
	$this->module->id,
);
?>


3. In AdminModule.php (or whatever your module's name) init() method:
$this->layoutPath = Yii::getPathOfAlias('admin.views.layouts');
$this->layout = 'main';


4. Extend your module's DefaultController from CController instead of Controller (as mentioned by Mr. Luke ).
5. Finally, customize your layouts by modifying the files in your module's 'layouts' folder.
0

#4 User is offline   stu2000 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 14-March 11

Posted 12 September 2012 - 03:35 AM

View PostVladimirV, on 07 November 2011 - 07:22 AM, said:

In case you're using the module generated by gii, you'll also need to do the following:
1. Copy the files from the main 'layouts' folder into your module's layouts folder (it is empty);
2. Remove from your module's views/default/index.php :
<?php
$this->breadcrumbs=array(
	$this->module->id,
);
?>


3. In AdminModule.php (or whatever your module's name) init() method:
$this->layoutPath = Yii::getPathOfAlias('admin.views.layouts');
$this->layout = 'main';


4. Extend your module's DefaultController from CController instead of Controller (as mentioned by Mr. Luke ).
5. Finally, customize your layouts by modifying the files in your module's 'layouts' folder.


Sorry to jump on an old thread, and it's really good advice, but I'd like to add one thing, in case anyuone's in the situation I'm in where I need a method located in the Controller class. Instead of extending the default controller from CController, you can still extend from Controller (as that extends from CController anyway), only you'll need to remove the following line from the Controller class;

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


as this is overwriting the value set in the init() method for the module controller
0

#5 User is offline   gia-web.com 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 09-September 13

Posted 19 September 2013 - 07:56 AM

Thank you VladimirV :)
0

#6 Guest_Interboy_*

  • Group: Guests

Posted 22 October 2013 - 10:41 PM

$this->layoutPath = Yii::getPathOfAlias('Backend.views.layouts');


It should add in init() function.

Moreover, we can set theme for new module:
Yii::app()->theme = 'newmoduletheme'; 

0

#7 User is offline   alinmircea 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 01-September 11

Posted 11 November 2013 - 07:15 PM

Wouldn't setting $layout in your Controller class to '/layouts/main'; instead of '//layouts/main'; just work?
(after copying the layout files into your module that is)

'//' means use the top-most one. while just '/' means the defined in your current module or 'application'
0

#8 User is offline   kasinau 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 04-December 13

Posted 04 December 2013 - 02:53 AM

To have a personalized layout for the module you also can create a components/Controller class in your module and extending it from CController class.
It is not a good idea to overwrite the layout property in the application Controller class because these changes will apply for the entire application, not only for the module.
Defining a Controller class for a specific module you will be able to customize the module's Controller class, without changing the entire application behaviour.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users