This time – a really short tip. In default Laravel Auth functionality there are some predefined values, settings and similar stuff – one of them is redirecting to /home URL if a user is already logged in. What if we don’t have that /home – what if in our case it’s different?
Basically, if you don’t have /home URL in your routes.php file, you will get something like this:
If you want to change that already-logged-in URL to a different one – let’s say, /account or just root URL /, you need this file:
app/Http/Middleware/RedirectIfAuthenticated.php:
public function handle($request, Closure $next) { if ($this->auth->check()) { return redirect('/home'); } return $next($request); }
Just change ‘/home’ here to whatever you want and that’s it. You can also use something like redirect()->route() or even some other functionality, not only redirect.
Update: thanks for the tip in the comments by Florian from Laramap (awesome project, by the way) and Francis – if you want to change the after-login URL, you should override a property in app/Http/Controllers/AuthController.php:
protected $redirectPath = '/dashboard';
According to the http://laravel.com/docs/5.1/authentication#authentication-quickstart i prefer to use “protected $redirectPath = ‘/dashboard’;” 🙂
Good point, Florian, will update the article.
This is not actually the same thing – $redirectPath is used to redirect once the user has logged in. The middleware is used when a user (already logged in) tries to access let’s say the login page.
Very not the same. Gotta change both still, but not the same at all.
Thanks for flagging, you’re right – looks similar but have different use-cases. Will update the article again.
What to do if i want to change content based on authentication satus. For Example, before authentication home page will have slider and other kind of static stuff but i want to fetch data from db if the user is logged in.
Thanks in Advance.
What if after having registered an account, redirect to login page, instead of home