Laravel 5 redirect to home page if attempting to go to login screen Laravel 5 redirect to home page if attempting to go to login screen php php

Laravel 5 redirect to home page if attempting to go to login screen


I did this in my router, although I'm not sure if it's the best solution:

Route::get('/', function () {    if(Auth::check()) {        return redirect('/dashboard');    } else {        return view('auth.login');    }});


You should use middleware for this.

Laravel 5 has already a 'guest' middleware out of the box you can use, so just doing the following should be enough:

Route::get('/', ['middleware' =>'guest', function(){  return view('auth.login');}]);

Then in the middleware file App\Http\Middleware\RedirectIfAuthenticated you can specify where the user is redirected to.

The default is /home.


When a user is successfully authenticated, they will be redirected to the /home URI, which you will need to register a route to handle. You can customize the post-authentication redirect location by defining a redirectPath property on the AuthController:

in your AuthController change the redirectPath property

protected $redirectPath = '/dashboard';

http://laravel.com/docs/5.1/authentication#Authenticating