代码之家  ›  专栏  ›  技术社区  ›  Inigo

在laravel5.6中,“绑定”中间件做什么?

  •  2
  • Inigo  · 技术社区  · 7 年前

    Kernel.php 作为:

    protected $middlewareGroups = [
        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];
    

    bindings 是的,我哪儿也找不到。

    它使用 SubstituteBindings 具有 handle

    public function handle($request, Closure $next)
    {
        $this->router->substituteBindings($route = $request->route());
        $this->router->substituteImplicitBindings($route);
        return $next($request);
    }
    

    尽管我还是不明白它是怎么做的。

    2 回复  |  直到 7 年前
        1
  •  14
  •   Yevgeniy Afanasyev    6 年前

    我也有同样的问题,我发现:

    Route model binding 现在使用中间件完成。全部 应用程序应添加 照亮\路由\中间件\SubstituteBindings到您的web app/Http/Kernel.php文件中的中间件组:

    您还应该为绑定替换注册路由中间件

    '绑定'=> \照亮\路由\中间件\替换索引::类,“…”

    https://laravel.com/docs/5.3/upgrade

    上面的答案来自这个来源- https://stackoverflow.com/a/47784205/3089840

    所以在我看来 bindings \Illuminate\Routing\Middleware\SubstituteBindings::class -如果这是正确的,我不知道为什么拉威尔不使用相同的术语在这两个领域 web 以及 api 中的数组 Kernel.php . 使用起来似乎有点前后矛盾和混乱 数组和 绑定 应用程序编程接口

        2
  •  7
  •   Mladen Janjetovic    6 年前

    我想你要的是这个 https://laravel.com/docs/5.7/routing#route-model-binding

    Route::get('api/users/{user}', function (App\User $user) {
        return $user->email;
    });
    

    它立即绑定用户模型。

    推荐文章