Just created a new admin module few times and it keeps using the newlayout theme we made for the rest of the site instead of showing it without any layout.
Added the modified layout files in the module layout folder and all the other changes as the book later suggests and it still overrides them and keeps using the same theme, any idea what do I do wrong? Thanks in advance.
Does your AdminModule specify the layout to use?..specifcally $this->layout = ‘main’?
<?php
class AdminModule extends CWebModule
{
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 = 'main';
}
Where this ‘main’ refers to /protected/modules/admin/views/layouts/main.php.
Yeah, and I followed the rest of the chapter and did all the other changes and everything else works good, the system message page uses the correct blue layout and all the rest works except the first admin page which doesn’t take the changes somehow.
The solution - for me anyway - was to add a line to beforeControllerAction in AdminModule.php:
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// this method is called before any module controller action is performed
// you may place customized code here
$controller->layout = 'main';
return true;
}
else
return false;
}
I think you can guess what the line is …
Nothing worked, until I threw in that line of code.
I had the same problem, but after trying jacmoes solution, the column1.php layout was not rendered anymore. In the Controller class (the one in the components folder), I made a simple change to the default $layout property as follows:
class Controller extends CController
{
//public $layout='//layouts/column1';
public $layout='/layouts/column1';
public $menu=array();
public $breadcrumbs=array();
}
Then you can set the layout in the init() method of your AdminModule.php as is described in the book.
By the way, im using version 1.1.4 for the trackstar application, maybe the default $layout property in 1.1.2 is different from the one in 1.1.4. Nevertheless, this should work for all versions.
After reading this full thread, I solved my problem. What I did, am writing following:
DefaultController.php :
class DefaultController extends Controller
{
public $layout='column1';
public function actionIndex()
{
//$this->layout='/layouts/main';
$this->render('index');
}
}
class AdminModule extends CWebModule
{
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 = 'main';
}
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// this method is called before any module controller action is performed
// you may place customized code here
return true;
}
else
return false;
}
}
writing the
public $layout='column1';
in the default controller will help to use different coloumn in the admin theme. If you use
$controller->layout = 'main';
in the beforeControllerAction in AdminModule.php will fix you to use only single layout. If you introduce new controller in admin module, just write this line
public $layout='column1';
at top of the controller as like DefaultController.php
<?php
class AdminModule extends CWebModule
{
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->layoutPath = Yii::getPathOfAlias('admin.views.layouts');
$this->layout = 'main';
}
public function beforeControllerAction($controller, $action)
{
if(parent::beforeControllerAction($controller, $action))
{
// this method is called before any module controller action is performed
// you may place customized code here
return true;
}
else
return false;
}
}
DefaultController.php
public $layout = ‘column1’;
<?php
class DefaultController extends Controller
{
public $layout = 'column1';
public function actionIndex()
{
$this->render('index');
}
}