重定向不适用于自定义中间件

时间:2019-12-28 14:53:19

标签: laravel laravel-5.8 laravel-6

我正在使用Laravel项目5.8版。

我使用了两个中间件AdminAuthor

我已经创建了AdminMiddlewareAuthorMiddlware

我都在Kernel.php中注册。我更改了RedirectIfAuthenticatedloginController进行自定义重定向。我创建了不同的路由组。

对于管理员中间件,它将重定向admin.dashboard,对于作者中间件,它将重定向到author.dashboard

然后,它将重定向到/home路由,就像默认的laravel登录用户一样。 我想在登录后将admin重定向到admin.dashboard,将作者重定向到author.dashboard 。自定义重定向不起作用。我多次查看了我的项目,但找不到问题。

loginController.php

// protected $redirectTo = '/home';
public function __construct(){
    if(Auth::check() && Auth::user()->role->id==1) {
        return redirect()->route('author.dashboard');
    } else if (Auth::check() && Auth::user()->role->id==2) {
        return redirect()->route('admin.dashboard');
    }else {
        $this->middleware('guest')->except('logout');
    }
}

RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null){
    if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
        return redirect()->route('author.dashboard');
    }else if (Auth::guard($guard)->check() && Auth::user()->role->id==2) {
        return redirect()->route('admin.dashboard');;
    } else {
        return $next($request);
    }
}

AuthorMiddleware

public function handle($request, Closure $next)
{
    if(Auth::check() && Auth::user()->role->id==1){
        return $next($request);
    } else {
        return redirect()->route('/login');
    }
}

AdminMiddleware

public function handle($request, Closure $next)
{
    if(Auth::check() && Auth::user()->role->id==2){
        return $next($request);
    } else {
        return redirect()->route('/login');
    }
}

Web.php

Route::group(['as'=>'author.','prefix'=>'author','namespace'=>'Author','middleware' => 'author'], function () {
Route::get('/dashboard','DashboardController@index')->name('dashboard');
});

Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware' => 'admin'], function () {
Route::get('/dashboard','DashboardController@index')->name('dashboard');
});

Author / DashobardController

public function index()
{
    return view('author.dashboard');
}

Admin / DashobardController

public function index()
{
    return view('admin.dashboard');
}

我是Laravel的新朋友。第一次遇到此问题,无法找到问题,请提供帮助。

2 个答案:

答案 0 :(得分:3)

您需要将以下几行编辑到LoginController.php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

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;


protected function authenticated(Request $request, $user)
{
    if(Auth::check() && Auth::user()->role->id==1) {
        return redirect()->route('author.dashboard');
    } else if (Auth::check() && Auth::user()->role->id==2) {
        return redirect()->route('admin.dashboard');
    }

    return redirect('/home');
}
/**
 * Where to redirect users after login.
 *
 * @var string
 */
//protected $redirectTo = '/admin';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}

答案 1 :(得分:-1)

更改

RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null){
    if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
        return '/author/dashboard';
    }else if (Auth::guard($guard)->check() && Auth::user()->role->id==2) {
        return 'admin/dashboard';
    } else {
        return $next($request);
    }
}

public function handle($request, Closure $next, $guard = null){
    if (Auth::user()->role->id==1) {
        return '/author/dashboard';
    }else if (Auth::user()->role->id==2) {
        return 'admin/dashboard';
    } else {
        return $next($request);
    }
}

Logincontoller.php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

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;


protected function authenticated(Request $request, $user)
{
    if(Auth::check() && Auth::user()->role->id==1) {
        return 'author/dashboard';
    } else if (Auth::check() && Auth::user()->role->id==2) {
        return 'admin/dashboard';
    }

    return redirect('/home');
}
/**
 * Where to redirect users after login.
 *
 * @var string
 */
//protected $redirectTo = '/admin';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}