这里为什么要用两次闭包呢,一层不就够了吗?

    protected function carry()
    {
        return function ($stack, $pipe) {
            return function ($passable) use ($stack, $pipe) {
                if (is_callable($pipe)) {
                    // If the pipe is an instance of a Closure, we will just call it directly but
                    // otherwise we'll resolve the pipes out of the container and call it with
                    // the appropriate method and arguments, returning the results back out.
                    return $pipe($passable, $stack);
                } elseif (! is_object($pipe)) {
                .....

                //这样不行吗
        return function ($stack, $pipe,$passable) {
                if (is_callable($pipe)) {
                    // If the pipe is an instance of a Closure, we will just call it directly but
                    // otherwise we'll resolve the pipes out of the container and call it with
                    // the appropriate method and arguments, returning the results back out.
                    return $pipe($passable, $stack);
                } elseif (! is_object($pipe)) {
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
falling-ts
最佳答案

额,能跑通,就代表应该是另一种方式;不过你这是通过传参方式实现 $stack 的迭代。官方通过挂载静态变量的方式迭代 $stack。静态变量感觉要好一些。因为就定义一遍,多个中间件使用。

4年前 评论
讨论数量: 4
falling-ts

不行的。因为 array_reduce 包含啦两层 闭包函数

4年前 评论
falling-ts

file

4年前 评论

可能没明白我我意思,子类改成这个

file

父类改成这个

file
并且试了一下,能跑通,以我就在想一层闭包能解决的事为什么要两层

4年前 评论
falling-ts

额,能跑通,就代表应该是另一种方式;不过你这是通过传参方式实现 $stack 的迭代。官方通过挂载静态变量的方式迭代 $stack。静态变量感觉要好一些。因为就定义一遍,多个中间件使用。

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!