if logic advice needed

Hi all,

I am new to Yii and OOP in general. I have installed yii and it’s behaving great so far. I decided to play around with it with the sole purpose to learn. What I am trying to do is prevent the login page from displaying if the user has already logged in. So after you login you can still call index.php?r=site/login and the login page would display asking for your user/pass no matter whether you are logged in or not. So what I did is in my login.php file (views/login.php) added the following logic:




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

{

....show login form

}else{

echo "You are already logged in!";

}



The above code works! However, I am not sure if I am following the MVC design pattern by adding the above logic in a view file. Can you please advise me if there is a better way to apply this logic rather than in the view file?

Cheers,

T.

Probably the most vital solution is to setup accessRules in SiteController and deny site/login for logged in users.

How about:




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

{

  $this->render('loginform', array(...));

}else{

  $this->render('alreadyloggedin', array(...));

}



This works brilliant. Thanks so much. Another question I have:

Yii renders the login page by:


$this->render('login',array('form'=>$form));

‘login’ - is the naming convention for the login.php file but I don’t understand the convention for the array(‘form’=>$form). $form is declared in the actionLogin but what is the convention for ‘form’(the key). If I change it to array(‘form1’=>$form) it won’t work. I searched for this all over but was unable to find the answer.

Thanks in advance,

T.

That’s the data that is passed to the view. array(‘form1’=>$form) will pass the data in $form to the view, but it will be accessible via $form1, not $form

I like to have my login pages simply throw an error if the user is already logged in. To do that, listen to what pestaa said.

http://www.yiiframework.com/doc/guide/topics.auth#access-control-filter

Thanks Jonah,

I tried to achieve this by accessRules and it works! Many thanks. I guess I need to learn how to customize the error messages now.

Thanks guys

T.

btw…what is the disadvantage (or potential negative impact) to go with thyseus suggestion above?

There’s none. IMO this all is a question of personal taste - and maybe how much the output for loginForm and alreadyLoggedIn differ. You could do the same in a single view:


<?php if (Yii::app()->user->isGuest): ?>

  <!--- Output alreadyLoggedIn here -->

<?php else: ?>

  <!--- Output login form here -->

<?php endif; ?>