Redirecting User from Login Page to Home Page if A

2019-09-11 17:34发布

I am building a login/register module using Laravel 5.1

I have defined the following routes

Route::get('/', function () {
return view('welcome');
});

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

I have also created a view for welcome and home. When I enter my credentials and login, I get redirected to the home page.

How do I make sure that once I am authenticated and I try to access the '/' route, I get redirected to the home page.

Currently once I am logged in and reach the home page and I type http://localhost:8000/ I get back to the login page even though I am still logged in. How can I prevent this?

3条回答
神经病院院长
2楼-- · 2019-09-11 17:44

In your controller:

 protected $redirectTo = '/home';

 public function __construct()
        {
            $this->middleware('guest:user', ['except' => ['logout']]);
        }

(with user is the auth logging in)

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-11 17:47

I would approach this the other way around. I would always go to home, and if they are not authenticated, redirect them to the login page.

查看更多
等我变得足够好
4楼-- · 2019-09-11 18:01

Check If user is authenticated and if user is authenticated then redirect to home

Route::get('/', function () {
   if(Auth::check()){return Redirect::to('home');}
    return view('welcome');
});
查看更多
登录 后发表回答