Laravel 5.4 auth 中间件覆盖 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方法用于在成功login/register后重定向用户。但是中间件有不同的功能,它们总是在进入(或离开)控制器之前(或之后)分析请求。

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