我在我的路由器中这样做了,虽然我不确定它是否是最好的解决方案:
Route::get('/', function () {
if(Auth::check()) {
return redirect('/dashboard');
} else {
return view('auth.login');
}
});
Run Code Online (Sandbox Code Playgroud)
小智 5
您应该为此使用中间件。
Laravel 5已经开箱即可使用“来宾”中间件,因此只需执行以下操作就足够了:
Route::get('/', ['middleware' =>'guest', function(){
return view('auth.login');
}]);
Run Code Online (Sandbox Code Playgroud)
然后,在中间件文件中,App\Http\Middleware\RedirectIfAuthenticated您可以指定用户重定向到的位置。
默认值为/home。
当用户成功通过身份验证后,他们将被重定向到 /home URI,您需要注册一个路由来处理该 URI。您可以通过在 AuthController 上定义 redirectPath 属性来自定义身份验证后重定向位置:
在你的AuthController中更改redirectPath属性
protected $redirectPath = '/dashboard';
Run Code Online (Sandbox Code Playgroud)
http://laravel.com/docs/5.1/authentication#Authenticating