Authentication and Profile form in the header (main layout)

Hi guys,

I am relatively new to the framework but I think I am catching up quickly.

I was wondering how to implement an authentication and profile form in the header located in the main layout.

If you first take a look at the picture ->

(Sorry for the semi-link but I am new therefore I am not allowed any image attachments or actual links)

You can see that I want to have a login-form and a profile-form which have to stay in the header on the right side.

Basically the login-form is shown when the context of the controller is an anonymous user and the profile-form is shown when the context of the controller is an authenticated user.

The thing is that I fiddled around with the main layout and basically I have created a login-form view and a profile-form view which I partially render in the main.php

// Anonymous user controller action

<div id="login-form">

  <?php echo $this->renderPartial('login-form', array('model'=>$this->loginForm)); ?>


// Authenticated user controller action

<div id="profile-form">

  <?php echo $this->renderPartial('profile-form', array('model'=>$this->profileForm)); ?>


What I find disturbing in this case is that I have to set $this->loginForm and $this->profileForm in the controller because when I render a page, the main layout does not have access to the appropriate model. I find this in a way, a bad practice and I have read some information on the yii website which states that I have to avoid dynamic data in the header, and that setting variables for the layout is done by some kind of registry. What can you tell me about that from your personal experience, what’s the best way to do it?

Second of all, I would like to ask, if there is a way for me to define those profile and login form views as some kind of modules and just include them whenever I need them in the appropriate views, and if so, what’s the best practice for doing so.

To summarize, the idea is that no matter what view I load from the controllers which are in the authenticated users’ context, I will always get the profile-form rendered in the header(apparently in the main layout), if a controller which is in the anonymous users’ context is invoked, then render the login-form in the header.

Thanks for the responses in advance!


Ivo :)

Make a widget. See the Guide for some examples of widgets - designed to solve exactly this problem.