Yii Framework Forum: Proper Way to Fork Layouts - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Proper Way to Fork Layouts Different layout for logged in/general public Rate Topic: -----

#1 User is offline   queej 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 04-February 10

Posted 06 February 2010 - 07:31 PM

I want to have a different layout for users who are logged in versus the general public. Where is the best place to make this decision? How/where would I tell the app to use layouts/main.php versus, say, layouts/genpublic.php?

Thanks.
0

#2 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 312
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 06 February 2010 - 09:38 PM

Hi Queej,

You can set which layout to use in your controller like this:

class ClientsController extends CController {

    //set default layout
    public $layout='loggedin';
...


In the '/protected/views/layouts/' folder, you should have a view (layout) by the same name, in this example that would be '/protected/views/layouts/loggedin.php'.

The access rules in your controller class would take care of who can access the controller.

EDIT:

A bit more info...

You can also set the layout in the 'action' of a controller like this:

public function actionAdmin()
{
    //set layout
    $this->layout='loggedin';
...


Hope this points you in the right direction.
0

#3 User is offline   queej 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 04-February 10

Posted 06 February 2010 - 10:01 PM

View Postoutrage, on 06 February 2010 - 09:38 PM, said:

Hope this points you in the right direction.


Yes, thanks. Just what I needed to get moving again!
0

#4 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 312
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 06 February 2010 - 10:26 PM

View Postqueej, on 06 February 2010 - 10:01 PM, said:

Yes, thanks. Just what I needed to get moving again!



Great!
Good luck and have fun with Yii.
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 07 February 2010 - 06:29 AM

Alternatively you might want to use a BaseController that all your controllers extend from. If you add a beforeAction() to that controller, it will be executed before every action. That way you have to implement the user status check only once:

// Make your controllers extend from this class
class BaseController extends CController
{
    public function beforeAction($action)
    {
        if (Yii::app()->user->isGuest)
            $this->layout='genpublic';

        return true; // If false, the action would not be executed...
    }
}


Not tested though. ;)
0

#6 User is offline   queej 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 04-February 10

Posted 09 February 2010 - 11:29 AM

That is a cool idea. I can see where that may come in handy in another place in my app. I am using one data model, but presenting it in several different ways, based on a "type" column. This could help me set up type shared information before invocation of a specific controller.

Thanks!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users