Return back to the page

Hi,

so I have a link on one of my pages

CHtml::link(‘You need to be logged in’, array(‘account/login’));} ?> </div>

that sends the user to the login page. I want it to redirect them back to the page after login. How can I go about doing this?

$this->redirect(Yii::app()->user->returnUrl);

Read this - http://www.yiiframework.com/doc/guide/1.1/en/form.action

Yes I understand this, but the returnUrl is the login page, because I’m allowing the user to navigate to the login page, so that becomes the returnUrl, if it was done with the accessControl it could work. Its different when you are giving a link to the user. So in a sense, I need the page right before the login page.

I give user the link to the login page.

User is at the Login Page -> THIS BECOMES THE RETURN URL

User Successfully logs in

Controller now redirects back to the returnUrl -> Which is the Login Page

This is the problem I’m facing :)

Let us assume the user want’s to leave a comment and his wish takes him to the action actionLeaveComment() of one of your controllers. But the user needs to be logged in before doing this. So you do a check

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

 &#036;this-&gt;redirect(&quot;Your login Url&quot;);

What i am trying to say is that the redirection to the login page should happen when the user tries to do some stuff, instead of you giving a link to the user to go to the login page. If you tell me the exact functional requirement, maybe i can help.

You can implement a referrer stack in Controller class.

You can push referrers at controller init() and pop whenever you want.

by Yii::app()->request->getUrlReferrer() you can access referrer link.

I’m doing my action via ajax, so I need it to be able to check first if user is logged in then redirect.

To check if a request is ajax or not, you can use -


Yii::app()->request->isAjaxRequest