[英]Source code of "Closure" class in Laravel
RedirectIfAuthenticated
middleware of Laravel uses a class called Closure. Laravel 的
RedirectIfAuthenticated
中间件使用了一个名为 Closure 的类。
Code:代码:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
Now, I need to find out the source code of that above mentioned Closure class.现在,我需要找出上述 Closure 类的源代码。 I'm passing hard times to understand how this
next()
method is functioning here.我很难理解这个
next()
方法在这里是如何运作的。
Thanks.谢谢。
In this particular case, you'll want to read up on Middleware .在这种特殊情况下,您需要阅读Middleware 。 They are basically a list/stack where you have various things happening before you get to the route handler (usually a controller).
它们基本上是一个列表/堆栈,在到达路由处理程序(通常是控制器)之前,您会在其中发生各种事情。 The
$next
parameter is a callback that calls the next middleware in the stack. $next
参数是调用堆栈中下一个中间件的回调。
To figure out what $next
is, you'll need to figure out the middleware that comes after RedirectIfAuthenticated
.要弄清楚
$next
是什么,您需要弄清楚RedirectIfAuthenticated
之后的中间件。 The easiest way to tell that is to php artisan route:list
and look at all the middleware for that route, then look at $middlewarePriority
.最简单的方法是
php artisan route:list
并查看该路由的所有中间件,然后查看$middlewarePriority
。 If the middlewares are not found in the priority variable, then middleware if first in first out, in that if you specify multiple middlewares for a route, the first one defined happens first.如果在优先级变量中没有找到中间件,则中间件为先进先出,因为如果您为一条路由指定多个中间件,则首先定义的第一个。
If you only have a single middleware on the route then you are getting to the route, unless you're using other things such asForm Requests .如果您在路线上只有一个中间件,那么您将进入该路线,除非您使用其他东西,例如Form Requests 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.