Laravel redirect to intended location after login
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
Interesting tidbit I discovered over the weekend when using the auth
filter in Laravel 4 to redirect users where they had originally intended to go. Before I had found this, I was passing the intended location encoded in the URL and then picking it up in the login form, which in hindsight was pretty darn silly.
If you actually look at the definition of the guest function of the redirector, you'll see that the intended location is actually saved into the session.
And furthermore, the redirector also has a simple function that can perform a redirect to that intended location.
This way, after having a user login, it's really easy to shoot them off to where they intended to go without having to write all your own code.
You need to pass a default location in case an intended location isn't provided in the session (which in this case would be the homepage). It's a really neat trick, I wish it was better documented, but it's always nice to stumble upon cute things like this by accident.