Setting Default Layout For All Controllers In Module [Solved]

Hello, I have created a Module with Gii named admin. changed default controller to:




class DefaultController extends CController // I have removed /protected/component/controller.php

{

	public function actionIndex()

	{

		$this->render('index');

	}

}

[b]

[/b]Then have set /protected/config/main.php:


	'modules'=>array(

		// uncomment the following to enable the Gii tool

		'gii'=>array(

			'class'=>'system.gii.GiiModule',

			'password'=>'pass',

			// If removed, Gii defaults to localhost only. Edit carefully to taste.

			'ipFilters'=>array('127.0.0.1','::1'),

		),

        'admin',

	),

Then have copy the /protected/views/layouts directory on protected/modules/admin/views/.

protected/modules/admin/views/layouts/admin.php:


beginContent('//layouts/main') ?>

    	<div id='admin' class="content">

    	<?php echo $content ?>

	</div>

endContent() ?>

Then i have overridden $layout property in protected/modules/admin/AdminModule.php so that all controllers(AdminModule->controllers) can use this layout:




class AdminModule extends CWebModule

{

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

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

		));

    }


	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;

	}

}



But when go ahead with http://localhost/web...dex.php?r=admin encountered with this error:


Property "DefaultController.layout" is not defined.	

How can i set default layout in AdminModule so that all controllers will can use that default layout?

set it in beforeControllerAction




public function beforeControllerAction($controller, $action)

	{

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

		{

			$controller->layout='admin';

			

			return true;

		}

		else

			return false;

	}




I before tried it but didn’t solve. I have searched accross the forum and the Net and encountered with many solutions but the solutions cannot help me.

this is a worked solution!! pls check the path of the layout

the layout should be in the module/views/layouts folder

try outside too




public function beforeControllerAction($controller, $action)

        {


                $controller->layout='admin';

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

                {

                        

                        

                        return true;

                }

                else

                        return false;

        }



try to add this line of code

public $layout=’//layouts/admin’;

in DefaultController.php, just above the function public function actionIndex()

My layout files are in /protected/modules/admin/views/layouts/admin.php

/protected/modules/admin/AdminModule :




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

		));

	}


	public function beforeControllerAction($controller, $action)

	{


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

		{

        	$controller->layout='admin'; //$controller->layout='/layouts/admin';

        	return true;

		}

		else

			return false;

	}

}



But dont work!!


Property "DefaultController.layout" is not defined. 




---

2013/06/03 13:57:12 [error] [exception.CException] exception 'CException' with message 'Property "DefaultController.layout" is not defined.' in C:\Apache2\htdocs\yii\framework\base\CComponent.php:174

Stack trace:

#0 C:\Apache2\htdocs\website\protected\modules\admin\AdminModule.php(24): CComponent->__set('layout', 'admin')

#1 C:\Apache2\htdocs\yii\framework\web\CController.php(264): AdminModule->beforeControllerAction(Object(DefaultController), Object(CInlineAction))

#2 C:\Apache2\htdocs\yii\framework\web\CWebApplication.php(283): CController->run('')

#3 C:\Apache2\htdocs\yii\framework\web\CWebApplication.php(142): CWebApplication->runController('admin')

#4 C:\Apache2\htdocs\yii\framework\base\CApplication.php(162): CWebApplication->processRequest()

#5 C:\Apache2\htdocs\website\index.php(14): CApplication->run()

#6 {main}

REQUEST_URI=/website/index.php?r=admin

---



check this link

http://www.yiiframework.com/forum/index.php/topic/17797-default-layout-in-cwebmodule/

I know that. I want share admin.php layout across controllers within AdminModule.php, as document explained:

don’t work!!

Hi @msoa

Try with this code


public function beforeControllerAction($controller, $action)

        {

                Yii::app()->theme = 'admin';

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

                {

                        

                        

                        return true;

                }

                else

                        return false;

        }

It works in my case :)

what is happening?

this is not a related error "Property "DefaultController.layout" is not defined. "

check your DefaultController

pls trubleshoot it

echo something here




public function beforeControllerAction($controller, $action)

        {


                echo 'Hi'; exit;




                $controller->layout='admin';

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

                {

                        

                        

                        return true;

                }

                else

                        return false;

        }



check it whats happening… it wil work thats the solution i found!!

[b]Solved

[/b]In CController as default public $layout property is commented. and when we extends a Controller from CController, the Controller have not a property named $layout.

Hence when we declare $layout in a class extended from CWebModule for defining default layout for all Controllers, this property have not effect on controllers.

So for defining default layout for all module controllers we need declare the $layout property in the controller. With doing this we can simply define default layout for all controllers. I hope that is helpful.

I am waiting for your comments. Also have a question, what’s the reason $layout commented in CController?