layout of module (again)

Hello, all!

My project don’t see layout. I can’t understand why.

This is my config/main.php




return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'test',


	'defaultController' => 'index',

	'modules'=>array(

		'default'=>array(

			'defaultController'=>'index',

			'layout'=>'main',

		)

	),


	'import'=>array(

		'application.models.*'

	),


	// application components

	'components'=>array(

		// uncomment the following to enable URLs in path-format

		'urlManager' => array(

			'urlFormat' => 'path',

			'showScriptName' => false,

			'rules' => array(

				'<controller:\w+>/<action:\w+>' => 'default/<controller>/<action>',

				'<controller:\w+>' => 'default/<controller>',

				'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',

				'<module:\w+>/<controller:\w+>' => '<module>/<controller>',

				'' => 'default',

			)

		),


		'errorHandler'=>array(

            'errorAction'=>'default/index/error',

        ),

		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				)

			),

		),

	),


	'params'=>array(

		// this is used in contact page

		'adminEmail'=>'asdfasdf@ya.ru',

	),


	'onBeginRequest' => function() {

		if (Yii::app()->request->isAjaxRequest)

			header ('Content-type: application/json');

	}

);



My default module:




<?php

class DefaultModule extends CWebModule {


	public function init() {

		

	}


	public function beforeControllerAction($controller, $action) {


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


			$role = User::getInstance()->getUserRole();

			$cName = $controller->getId();

			$aName = $action->getId();

			$rules = array(

				'works' => array(

					'list' => array('guest', 'author', 'client', 'moderator'),

					'albums' => array('guest', 'author', 'client', 'moderator'),

					'profile' => array('author', 'client', 'moderator')

				),

				'author' => array(

					'works' => array('guest', 'author', 'client', 'moderator')

				),

				'index' => array(

					'login' => array('guest', 'author', 'client', 'moderator'),

					'logout' => array('author', 'client', 'moderator'),

					'authorise' => array('guest')

				)

			);


			if (

					isset($rules[$cName][$aName]) && 

					in_array($role, $rules[$cName][$aName])

			) {

				return true;

			}

			else {

				echo "Permission to path $cName/$aName denied.";

				return false;

			}

		}

		else

			return false;

	}

}



If I put


 echo $this->layoutPath; 

in DefaultModule I see /var/www/protected/modules/default/views/layouts

And in this directory I have file main.php. It’s my layout. When I visit any controller/action project don’t use layout.

Can u help me? Where I can see log error of finding layout or where I can find supplementary information.

Every controllers does not have definition of layout in init() method.

Thank you very much.

if u want to use different layout use $this->layout=‘layout name’; in action…and place that layout under /var/www/protected/views/layouts… or u give ur path.

I want to use one layout for all project. And all settings of project I wrote before. It’s not works for one layout, how I’ll set up it for working with different layout is a second question.

Why it not works for one layout?

i agree this is tricky

you have two things here to look at:

1- you have two layouts here one is for the module and the other is controller layout.

2- every controller under your Default module is extending from Controller (by default Gii Module generator) which is located under : protected/component

and that Controller class override the layout property.

hummm consider this two hints, it may help you

1- in your DefaultModule Class add $controller->layout = $this->layout;


<?php 

class DefaultModule extends CWebModule { 

...

  public function beforeControllerAction($controller, $action) {


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

      ...

      $controller->layout = $this->layout;

      ...

    }

    else

      return false; 

    }

...

}

2- in your config file main.php this line maybe helpful to you in order to acheive your goal


 'modules'=>array( 

    'default'=>array( 

      'layoutPath' => 'protected/views/layouts', // or any layoutPath you may need <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/wink.gif' class='bbc_emoticon' alt=';)' />

  )

),




I read comments in generated by Yii code:




// renders the view file 'protected/views/site/index.php'

// using the default layout 'protected/views/layouts/main.php'

$this->render('index');



and reason of my trouble is none writing $this->render(‘index’);

Thank you, guys.