Laravel 5.4 auth中间件会覆盖redirectTo方法问题

时间:2017-11-12 05:02:21

标签: php laravel laravel-5.4

我正在使用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';  
        
    }

}
&#13;
&#13;
&#13;

RedirectIfAuthenticated.php

&#13;
&#13;
<?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);
    }
}
&#13;
&#13;
&#13;

web.php

&#13;
&#13;
<?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');
&#13;
&#13;
&#13;

我在dd()语句中写了一个if函数,它确实有效。问题是无论如何,重定向总是转到/home

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

如果没有中间件覆盖该方法,我将如何能够依赖此重定向方法?

由于

1 个答案:

答案 0 :(得分:2)

您需要根据自己的条件更改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方法中,或者至少使用redirectTo特征中提供的AuthenticatesUsers属性提及重定向路由。其他laravel将您重定向到默认/home路由。 这仅在用户提交登录表单时发生,如果要将已登录的用户重定向到其他位置(此处为路由/employer),则必须使用中间件对其进行定义。