【问题标题】:redirect is not working for custom middleware重定向不适用于自定义中间件
【发布时间】:2020-04-18 02:03:54
【问题描述】:

我正在使用 Laravel 项目 5.8 版。

我使用了两个中间件AdminAuthor

我已经创建了AdminMiddlewareAuthorMiddlware

我都在Kernel.php 注册了。我将RedirectIfAuthenticatedloginController 更改为自定义重定向。我创建了不同的路线组。

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

之后,它会重定向到 /home 路由,就像默认的 laravel 登录用户一样。 我想在登录后将管理员重定向到 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);
    }
}

作者中间件

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

管理中间件

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');
});

作者/DashobardController

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

管理员/DashobardController

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

我是 Laravel 的新手。第一次遇到这个问题,找不到问题请帮忙。

【问题讨论】:

  • 登录后是否重定向到/home
  • 我想在登录后将管理员重定向到admin.dashboard,并将作者重定向到author.dashboard

标签: laravel laravel-5.8 laravel-6


【解决方案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']);
}
}

【讨论】:

  • logincontroller.php也有同样的变化
  • 仍然重定向到/home 路由。
  • 你的登录控制器有变化吗??
  • /author/dashboard 工作正常,但登录后不会重定向到此。我检查了你的代码。我更改了 loginController 和 redirectiifautheticate。
  • 还有其他文件需要更改吗?还有其他位置吗?
【解决方案2】:

您需要将以下行编辑到您的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']);
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2013-03-04
    • 2021-10-02
    相关资源
    最近更新 更多