简体   繁体   English

127.0.0.1 将您重定向了太多次。 ERR_TOO_MANY_REDIRECTS Laravel 8

[英]127.0.0.1 redirected you too many times. ERR_TOO_MANY_REDIRECTS Laravel 8

I want to manage the authorization in the routes so I create a middleware called " role " This is the role middleware handle function:我想管理路由中的授权,所以我创建了一个名为“角色”的中间件,这是角色中间件处理函数:

public function handle(Request $request, Closure $next)
{
    $role = Auth::user()->role_id;
    $emp_id = Auth::user()->employee_id;
    $clt_id = Auth::user()->client_id;
    if ($role == 3745639763)
    {
        return $next($request);

    } else if ($role == 1 && $emp_id == NULL)
    {
       return redirect('employee/con-reg');

    } else if ($role == 2 && $clt_id == NULL)
    {

        return $next($request);

    } else if ($role == 1 && $emp_id != NULL)
    {

        return redirect('client/con-reg');

    } else if ($role == 2 && $clt_id != NULL)
    {

        return $next($request);

    }
}

This is my routes for the employee:这是我给员工的路线:

Route::group([
'prefix'=> 'employee',
'middleware' => 'role',
'as' => 'employee.'
], function () {
    Route::get('/con-reg', function () {
        $works = Work::all();
        return view('employee/con-register', compact('works'));
    })->name('con-reg');

    Route::post('/store', [EmployeeController::class, 'store'])->name('store');
    Route::post('/profile', [EmployeeController::class, 'index'])->name('index');
});

and This is the home route, after the login or register you will be redirect to the home page, and this is where I also put my middlware because he can access the home after continue his registration with giving us more needed Information for his chosen role:这是主页路线,在登录或注册后,您将被重定向到主页,这也是我放置中间件的地方,因为他可以在继续注册后访问主页,并为我们提供他选择的角色所需的更多信息:

Route::view('/home', 'home')->middleware('role');

This is Kernal.php file:这是Kernal.php文件:

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    'emp' => \App\Http\Middleware\emp::class,
    // 'clt' => \App\Http\Middleware\clt::class,
    'role' => \App\Http\Middleware\role::class, //<------This is My Middleware
];

so now When I Logged in it didn't redirect me to the con-register view, but instead of it the error above appears to me.所以现在当我登录时,它没有将我重定向到con-register视图,而是上面的错误出现在我面前。

It's because route employee/con-reg protected with this middleware.这是因为路由员工/con-reg受此中间件保护。 After redirect - you go into the loop check->redirect You need to disable role middleware for route employee/con-reg重定向后-您进入循环检查->重定向您需要禁用路由员工/con-reg的角色中间件

Fast solution:快速解决方案:

if ($request->path() === 'employee/con-reg'){
   return $next($request);
}

Right solution:正确的解决方法:

Put this action in controller (not in Closure, it's bad practice) and disable middleware for it将此操作放在控制器中(不是在 Closure 中,这是不好的做法)并为其禁用中间件

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

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