繁体   English   中英

Laravel 5.4身份验证中间件覆盖redirectTo方法问题

[英]Laravel 5.4 auth middleware overrides redirectTo method issue

我正在使用Laravel Auth支架。 在我的LoginController.php文件中,有我的redirectTo方法。 我想要它有条件重定向。 但是,每次测试登录路由时,该路由都会到达/home而不是在条件条件中指定的路由。

LoginController.php

 <?php namespace App\\Http\\Controllers\\Auth; use Auth; use App\\Role; use App\\Http\\Controllers\\Controller; use Illuminate\\Foundation\\Auth\\AuthenticatesUsers; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Create a new controller instance. * * @return void= */ public function __construct() { $this->middleware('guest')->except('logout'); } protected function redirectTo() { $user = Auth::user(); if($user->isEmployer()){ return '/employer'; } return '/home'; } } 

RedirectIfAuthenticated.php

 <?php namespace App\\Http\\Middleware; use Closure; use Illuminate\\Support\\Facades\\Auth; class RedirectIfAuthenticated { /** * Handle an incoming request. * * @param \\Illuminate\\Http\\Request $request * @param \\Closure $next * @param string|null $guard * @return mixed */ public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return redirect('/home'); } return $next($request); } } 

web.php

 <?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/employer', 'EmployerController@index'); Route::get('/home', 'HomeController@index')->name('home'); 

我在if语句中编写了一个dd()函数,它确实起作用。 问题是无论如何重定向始终都转到/home

guest身份验证中间件将始终覆盖该功能时,我不明白要使用redirectTo方法吗?

在不让中间件覆盖该方法的情况下,我将如何能够依赖于此重定向方法?

谢谢

您需要根据您的条件更改RedirectIfAuthenticated中间件。 像这样:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        if(Auth::guard($guard)->user()->isEmployer())
            {
               return redirect('/employer');

             } else return redirect('/home');
    }

    return $next($request);
}

redirectTo方法用于在成功登录/注册后重定向用户。 但是中间件具有不同的功能,它们总是在进入(或离开)控制器之前(或之后)分析请求。

我想您已经知道这一点,您所缺少的是: 如果要在成功登录后重定向用户,则必须将逻辑放在redirectTo方法中,或者至少使用AuthenticatesUsers特性中可用的redirectTo属性提及要重定向的路由。其他方式laravel会将您重定向到默认/home路由。 这仅在用户提交登录表单时发生。如果要将已登录的用户重定向到其他位置(此处为route /employer ),则必须使用中间件进行定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM