自定义中间件 - 太多重定向 - Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47080545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Middleware - Too Many Redirects - Laravel
提问by Victori
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.
我想创建一个自定义中间件,只有当用户通过身份验证并且电子邮件是访问 /admin 页面的特定电子邮件时。
Although, when I specify my custom route and then a redirect it always says too many redirects..
虽然,当我指定我的自定义路由然后重定向时,它总是说重定向过多..
Short Explanation.
简短的解释。
- User Logs in -> redirected to /home. (Works)
- If user tries to access /admin and their email isn't like the one specified in the middleware, redirect to /home.
- If its true, let them in /admin
- 用户登录 -> 重定向到 /home。(作品)
- 如果用户尝试访问 /admin 并且他们的电子邮件与中间件中指定的不同,则重定向到 /home。
- 如果是真的,让他们进入 /admin
My middleware is called 'admin.verify'
我的中间件叫做“admin.verify”
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.
Routes 文件会自动加载,如果我执行 redirect('/home') 它会自动运行我的中间件,这就是为什么我猜测它经常重定向到主页。
Routes File:
路线文件:
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');
}
}
Middleware:
中间件:
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::User()->email == '[email protected]') {
return $next($request);
} else {
return redirect()->route('home');
}
My Home Route:
我的家乡路线:
GET|HEAD | home | home| App\Http\Controllers\HomeController@index | web,auth
Home Controller:
家庭控制器:
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');
}
}
采纳答案by Devon
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:
正如评论中所讨论的,您已经在您的全局中间件堆栈下注册了它,该堆栈在每个请求上运行。意思是,如果第一个条件失败,您将不断重定向到“home”,因为该中间件将在“home”(以及其他所有)路由上运行。所以你会去:
/some/page ... condition failed: redirect 'home'
/home ... condition failed: redirect 'home'
/home ... condition failed: redirect 'home' ... and so on
Inside app/Http/Kernel.php, you have three sections:
在 app/Http/Kernel.php 中,您有三个部分:
$middleware, the global middleware stack (runs on every request)
$middleware,全局中间件堆栈(在每个请求上运行)
$middlewareGroup, runs on every request for the group (web, api, etc). Anything in routes/web.php will run through the 'web' group.
$middlewareGroup,针对组的每个请求(web、api 等)运行。route/web.php 中的任何内容都将通过 'web' 组运行。
$routeMiddleware, route specific middleware which can be enabled on specific routes.
$routeMiddleware,可以在特定路由上启用的路由特定中间件。