Save url before login and redirect afterwards

I have an application that must only be accessed by authenticated users (except for a few actions). What I need is to save the url that an anonymous user requests before redirecting him to the login screen and after successful login to redirect him to the initial url. I did the first part (save the requested url), but I can’t manage to do the second part (redirect him to the url after the login).

If it helps, I don’t use the core access filters (accessRules()) method, but instead, for each controller, I override the run() method and check inside if the user is logged in and redirect him to the login screen.

So, is there a way to implement this?

What’s the problem using accesRules()? Return url would be stored then automatically in CWebUser::$returnUrl, and you would be able to perform the redirect with one line of code:


At this point, the problem with using accessRules() is that the application is at the final stages of its development and it’s time consuming to change the logic of the access rules.

So, any ideas are welcome…

Perfectly valid reason :)

$this->redirect() in your controller should work both with an url string and with a route/params array as its parameter. What kind of problem did you have?

Never mind. It was a mistake of mine, I did the redirect action at the wrong point inside the code.

Thanks for the help! :)

All you describe (including redirect + returning back to original action) is already implemented in Yii. You don’t need accessRules. Just put this on top of your controller action:

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


Thanks, I didn’t know that.