Best way to change view on condition


I have a site where user can view a page with list of some items, but only a user that is logged in can view this list, when a user isn’t logged in the same page should display only login form.

My question is what is the best way to change the viewed content based on condition?

For this example i could just send a bool variable to render method and in view make a simple if to show list or login form but is this the best practice ?

1 Like

You might want to take a look at this:


No, i don’t see a mechanism with CAccessControlFilter to view something else on view if user has no access.

It is a fairly common scenario, i don’t believe some more experienced users don’t know some best practices about this, will wait for more suggestions.

That is exactly what you should use to redirect users to a LoginForm if they should be logged in. Use the CAccessControlFilter (accessRules) to define what is visible and what is not, to anonymous or authenticated Users. If anonymous users are not allowed to see a specific action/view they will be redirected.

The thing is i don’t looking for a redirect mechanism, please read carefully what is my problem. The login form or anything else should be displayed on the same page, without redirect.

Can CAccessControlFilter decide which action to run based on user is logged in or not, assuming that the request is always for the same url.

After all it is not my problem, my problem is how in more elegant way (better that if else in view) switch content to show on the view, i can have more views and based on login status just show the appropriate view, and this is better but i can have also more such regions on one view that needs to be switched on/off. Is using [if else] statements the best way to do this or maybe a better options for this ?

The advice of yodel is correct: with CAccessControlFilter you can specify rules about what a user can do.

In your case you will allow authenticated user to see the list. If a not authenticated use will try to access the page, it will be redirected to the login form and, after a successfully login, will be redirected back to the page.

This solution is already implemented and 100% operative, you have just to read the guide and configure your controller.

No, that is not what i want, there can’t be any REDIRECTION, in this scenario I’m looking for a best practice to show or hide some parts of view bested on some conditions.

I will have one action controller that will check if user is logged in then if he is logged in i will set [show] variable to true if not, i will set [show] to false, after that i will make an [if else] on [show] variable in the view, if [show] will be true i will renderpartial a list if it will be false i will make an renderpartial login form, is there better way to do this than using [if else] in view ? I can not express more precise than this.

If I am understanding this correctly, maybe this would point you in the right direction.

Say on every page you want to display a login item if not logged in and a list of options if they are logged in. In the gii skeleton, the main menu has this with the login/logout menu items. Whoever you don’t need to use the menu options.

You could place code anywhere in your views like the following:

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


} else {



If you want it on every page, place it in [font="Courier New"]views/layout/main.php[/font] The check (isGuest) in the above code can easily be modified for any check that your authentication system provides.

Does this help?

Sorry for my long absence.

Yes jkofsky, this really helps me and it pointed me to another direction.

I think i would like to embed something into view and this thing would have it’s own view and controller.

So if i embed, a special global portlet it will itself check for example that user is logged in and display nothing if not, this way no [if else] in view markup, i just embed this in view and forget.

Thx for every answer.

Just to say thanks for this, just what I was looking for too. Same reason as the OP.