如何在另一个中间件中使用'Clousre $next'调用Laravel中间件的handle()方法?

问题描述 投票:0回答:2

这里是来自 Laravel 的 ValidatePostSize 的 handle() 方法:

public function handle($request, Closure $next)
{
    $max = $this->getPostMaxSize();

    if ($max > 0 && $request->server('CONTENT_LENGTH') > $max) {
        throw new PostTooLargeException;
    }

    return $next($request);
}

现在,使用另一个中间件的 $next($request) 调用此方法。我的理解是 handle() 方法被翻译为 $next。我想知道这是如何发生的。

php laravel laravel-middleware
2个回答
13
投票

要将请求更深入地传递到应用程序中(允许中间件“传递”),只需使用 $request 调用 $next 回调即可。 https://laravel.com/docs/5.4/middleware#defining-middleware

当 Laravel 处理请求时,它会运行堆栈中所有适用的中间件。中间件可以设置为在路由/控制器方法之前和/或之后运行。

为了能够做到这一点,Laravel 使用

Illuminate\Pipeline\Pipeline
。本质上,它使用
array_reduce
迭代中间件堆栈,然后返回
Closure
来执行该中间件。这样做的美妙之处在于使用
array_reverse
允许将下一个中间件执行传递给前一个中间件。


详细说明一下:

当调用

Illuminate\Foundation\Http\Kernel@handle
时,它会使用
sendRequestThroughRouter
建立响应,其中包含以下内容:

return (new Pipeline($this->app))
            ->send($request)
            ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
            ->then($this->dispatchToRouter());

Pipeline
Illuminate\Routing\Pipeline
延伸
Illuminate\Pipeline\Pipeline

上面的

then()
方法本质上是:

->then(function ($request) {
    $this->app->instance('request', $request);

    return $this->router->dispatch($request);
})

Then 意味着我们从接受最终结果的闭包开始(请记住,此时闭包不会被调用)。

然后,在

then()
方法中,就会发生上面提到的
array_reduce
array_reverse
部分。


这是

then()
方法中实际发生的情况的简化示例(假设您知道
array_reduce
的工作原理):

function then(Closure $destination)
{
    $pipeline = array_reduce(

        array_reverse($this->middlewares),

        //Remember $nextClosure is going to be the closure returned 
        //from the previous iteration

        function ($nextClosure, $middlewareClass) {

            //This is the $next closure you see in your middleware
            return function ($request) use ($nextClosure, $middlewareClass) {

                //Resolve the middleware
                $middleware = app($middlewareClass);

                //Call the middleware
                return $middleware->{$this->method}($request, $nextClosure);
            };
        },

        //The finial closure that will be called that resolves the destination

        function ($request) use ($destination) {
            return $destination($request);
        }
    );

    return $pipeline($this->request);
}

假设我们有 3 个中间件:

[
    One::class,
    Two::class,
    Three::class,
];

上面的

$pipeline
变量基本上是:

function ($request) {

    return app(One::class)->handle($request, function ($request) {

        return app(Two::class)->handle($request, function ($request) {

            return app(Three::class)->handle($request, function ($request) {

                return $destination($request);

            });
        };);
    };);
};

2
投票

接下来是一个闭包,一个匿名函数的变量。在您的代码中,您有

return $next($request);
。 $next 是一个闭包,基于第二个方法参数。这意味着您的方法的返回值是匿名函数返回的值。

例如:

// this is the Closure.
$next = function ($parameter) {
    return $parameter . ' This message is modified by $next';
};

public function handle($message, Closure $next)
{
    return $next($message);
}

// test the output
$message = 'Hello World!';
echo handle($message, $next); // output will be 'Hello World! This message is modified by $next'
© www.soinside.com 2019 - 2024. All rights reserved.