自定义中间件-重定向过多-Laravel [英] Custom Middleware - Too Many Redirects - Laravel

查看:53
本文介绍了自定义中间件-重定向过多-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义中间件,仅当用户通过身份验证并且电子邮件是用于访问/admin页面的特定电子邮件时.

I want to create a custom middleware that only if the user is authenticated and the email is a certain email to access the /admin page.

尽管,当我指定我的自定义路由然后进行重定向时,它总是显示过多的重定向.

Although, when I specify my custom route and then a redirect it always says too many redirects..

简短说明.

Short Explanation.

  1. 用户登录->重定向到/home.(工作)
  2. 如果用户尝试访问/admin并且其电子邮件与中间件中指定的电子邮件不同,请重定向至/home.
  3. 如果属实,请让他们进入/admin

我的中间件叫做'admin.verify'

My middleware is called 'admin.verify'

会自动加载Routes文件,如果我进行redirect('/home'),它将自动运行我的中间件,这就是为什么我猜到它经常重定向到首页的原因.

The Routes file is automatically loaded and If I do redirect('/home') it automatically runs my middleware which is why I'm guessing it redirects to homepage too often.

路由文件:

Route::get('/admin', 'AdminController@index')->name('admin.index');

AdminController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function __construct(){
      $this->middleware(['auth', 'admin.verify']);
    }


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

中间件:

 public function handle($request, Closure $next)
    {

      if (Auth::check() && Auth::User()->email == 'Tester@gmail.com') {
        return $next($request);
      } else {
        return redirect()->route('home');
      }

我的家庭路线:

 GET|HEAD | home | home| App\Http\Controllers\HomeController@index | web,auth

家庭控制器:

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }
}

推荐答案

如注释中所述,您已经在全局中间件堆栈下注册了该组件,该堆栈在每个单个请求上运行.这意味着,如果您在第一个条件失败,将不断重定向到"home",因为该中间件将在"home"(以及所有其他)路由上运行.所以你会去:

As discussed in the comments, you had registered this under your global middleware stack which runs on every single request. Meaning, you would redirect to 'home' constantly if you failed the first condition because this middleware would be run on the 'home' (and every other) route. So you'd go:

/some/page ... condition failed: redirect 'home'
/home ... condition failed: redirect 'home'
/home ... condition failed: redirect 'home' ... and so on

在app/Http/Kernel.php内部,您可以分为三个部分:

Inside app/Http/Kernel.php, you have three sections:

$ middleware,全局中间件堆栈(在每个请求上运行)

$middleware, the global middleware stack (runs on every request)

$ middlewareGroup,在对该组的每个请求(Web,API等)上运行.route/web.php中的所有内容都将通过网络"组运行.

$middlewareGroup, runs on every request for the group (web, api, etc). Anything in routes/web.php will run through the 'web' group.

$ routeMiddleware,可在特定路由上启用的特定路由中间件.

$routeMiddleware, route specific middleware which can be enabled on specific routes.

这篇关于自定义中间件-重定向过多-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆