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.
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.
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.
Yes, thanks. Just what I needed to get moving again!
Great!
Good luck and have fun with Yii.
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.
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!