How can I display a different main.php layout if the user is a guest?

Hi guys,

I’m just wondering how I can go about displaying a different main.php layout file to guests? So my end result would be having two main layout views that I can switch between, depending whether the user is logged in or not?

Thanks

In the protected/components/controller.php:




public function init() {

  if (Yii::app()->user->isGuest)

    $this->layout = '//layouts/public';

  else

    $this->layout = '//layouts/private';

}



In the views/layouts create the public.php and private.php files. Copy/paste the content from column1.php and modify as needed. You may want to create also separate main.php files, like main_public.php and main_private.php

in /Layout/main.php just Put Condition code [color=#660066][size=2]Yii[/size][/color][color=#666600][size=2]::[/size][/color][size=2]app[/size][color=#666600]size=2->[/size][/color][size=2]user[/size][color=#666600][size=2]->[/size][/color][size=2]isGuest then Other Lay out[/size]

[size=2]

[/size]

Hi,

You can assign different layouts for different actions or different controllers. If you want to change the default layout to some other layout create your layout under protected/views/layouts/yourlayout.php. Then call it in the Controller or action. For example if you want to call it in an action then just declared like this.


public function actionIndex()

	{

		if(Yii::app()->user->isGuest)

		{

			$this->layout = '//layouts/yourlayout'; // do not include .php

                }

        }

If you want to declare specific layout for the whole controller the specify


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

or as szfjozsef specified.

[size="3"]If you want to change layout just change in your controller file below code,[/size]

[size="2"]


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

[/size]

[size="2"]If you want to change main layout, change below code in layout file,[/size]

[size="2"]


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

[/size]

[size="2"][b]

[/b][/size]