我有一个laravel应用程序
登录页面在路线/登录
有一个登录用户并点击登录按钮(或基本上打开的URL /登录)
应用程序将用户重定向到/ home,但我想重定向到/ dashboard
我将Auth控制器中的重定向字段更改为/ dashboard。当用户登录时,应用程序会将他重定向到/ dashboard 但是登录用户呢?
我使用laravel 5.4,谢谢你的帮助
答案 0 :(得分:1)
转到位于
的登录控制器app->Http->Auth->LoginController
设置
protected $redirectTo = '/dashboard'
希望它有效。
来源:https://laravel.com/docs/5.4/authentication#included-authenticating
答案 1 :(得分:1)
您应该使用位于RedirectIfAuthenticated
的Laravel随附的App\Http\Middleware\RedirectIfAuthenticated
中间件。
然后,在下面的块中,确保将其设置为/dashboard
。
if (Auth::guard($guard)->check()) {
return redirect('/dashboard');
}
然后通过将中间件包装在一个组中来将中间件添加到您的登录路由中:
Route::group(['middleware' => 'guest'], function(){
//Auth routes for non-authenticated users
});
或者您可以直接在路线上进行:
->middleware('guest');
答案 2 :(得分:0)
由于您要重定向登录用户,您可以覆盖showLoginForm()
中的LoginController
方法,如下所示:
public function showLoginForm()
{
if (auth()->check()) {
return redirect('/dashboard');
}
return view('auth.login');
}
但我想解决问题的更好方法可能是隐藏登录用户的登录按钮或链接:
@if (auth()->guest())
{{-- Show register and login links --}}
@endif