Chapter 12 - Admin module automatically uses the site theme

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.

Could you confirm the version of the Yii framework you are using when having this issue? 1.1.2?, 1.1.3?, 1.1.4?

Thanks.

1.1.2 and as far as I know I did everything by the book, anything else to check for?

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 system msg url is "http://localhost/trackstar/admin/sysMessage/index" and the admin page one is "http://localhost/trackstar/admin/default/index" but the admin page keeps the same links as when it was first made instead of "Back to main site" "SystemMessage" "Login" as it should be.

Every time I create a test new module it automatically takes the site layout instead of using no layout, that seems to be the problem.

I have the same probleme!

Did you find a solution?

Had the same problem.

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 and this solution work for me thanks!!

yeah, sorry for the late response but it did work for me too, thanks, could put it as solved I guess.

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.

jacmoe thank you. your solution worked for me also =))

Hello,

I have Yii version 1.1.8 and the view file modules/admin/view/layouts/column1.php was ignored during view rendering. jacmoe’s solution for adding


$controller->layout = 'main'; 

to AdminModule::beforeControllerAction() did help for showing the proper (classic) theme, but the that rendered only the files:

modules/admin/views/index.php and modules/admin/views/layouts/main.php.

Why bother when the content is corrent? The parsed HTML lacked the lines:


<div class="container"><div id="content">

which set a nice margin around the content and that file was supposed to be used…

The fix for me was to change the following:

  1. In AdminModule::beforeControllerAction() change

$controller->layout = 'main'; 

to


$controller->layout = 'column1';

  1. In modules/admin/views/layouts/column1.php change

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

(as the book suggested) to


<?php $this->beginContent('/layouts/main'); ?>

Hope this helps…

Edit: Later on when column2.php comes into action,


$controller->layout = 'column1';

in AdminModule::beforeControllerAction() will have to become


$controller->layout = 'column2';

Thank you Yasen,

works for me on Yii 1.1.9.

Hi. I cannot display the colum2 in the System Message.

If I put


$controller->layout = 'column2';

in AdminModule::beforeControllerAction(), it’s change the layout to site layout.

Someone can do it works?

Another error.

In the page 304, has a call to class SysMessage like following:


//get the latest system message to display based on the

update_time column

$sysMessage = SysMessage::model()->find(array(

'order'=>'t.update_time DESC',

));

if($sysMessage != null)

$message = $sysMessage->message;

else

$message = null;

$this->render('index',array(

'dataProvider'=>$dataProvider,

'sysMessage'=>$message,

));



But I can’t render it in my view. It’s diplay an error “Undefined variable: sysMessage”. How can show some data in my view?

If I want create a ‘test’ => 'This is a test" How do it?

change first line in column2.php to this $this->beginContent(’/layouts/main’);

and DefaultController.php

<?php

class DefaultController extends Controller

{

public function actionIndex()


{


	&#036;this-&gt;layout='/layouts/column1';


	&#036;this-&gt;render('index');


}

}

?>

Hi Everyone,

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

Thanks

this is what worked for me (Yii ver 1.1.14)

AdminModule.php

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

$this->layout = ‘main’;[/b]


<?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');

	}

}