“绑定”中间件在 Laravel 5.6 中有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51612727/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:56:46  来源:igfitidea点击:

What does "bindings" middleware do in Laravel 5.6?

laravellaravel-5middlewarelaravel-middleware

提问by Inigo

Just as per the title. Default api middleware in Laravel 5.6 is listed in Kernel.phpas:

就像标题一样。Laravel 5.6 中默认的 api 中间件Kernel.php如下所示:

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

I'd appreciate a layman's explanation of what bindingsdoes, which I can't find anywhere.

我很感激外行对什么的解释bindings,我在任何地方都找不到。

It uses the SubstituteBindingsclass which has the handlemethod:

它使用SubstituteBindings具有以下handle方法的类:

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

Though I still don't follow what it does.

虽然我仍然不遵循它的作用。

回答by user3089840

I had the same question, and was able to find this:

我有同样的问题,并且能够找到这个:

"Route model bindingis now accomplished using middleware. All applications should add the Illuminate\Routing\Middleware\SubstituteBindings to your web middleware group in your app/Http/Kernel.php file:

\Illuminate\Routing\Middleware\SubstituteBindings::class,

You should also register a route middleware for binding substitution in the $routeMiddleware property of your HTTP kernel:

'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, ..."

"Route model binding现在使用中间件完成。所有应用程序都应该将 Illuminate\Routing\Middleware\SubstituteBindings 添加到您的 app/Http/Kernel.php 文件中的 web 中间件组:

\Illuminate\Routing\Middleware\SubstituteBindings::class,

您还应该在 HTTP 内核的 $routeMiddleware 属性中注册一个用于绑定替换的路由中间件:

'绑定' => \Illuminate\Routing\Middleware\SubstituteBindings::class, ..."

which can be found on this page - https://laravel.com/docs/5.3/upgrade

可以在此页面上找到 - https://laravel.com/docs/5.3/upgrade

The above answer was originally from this source - https://stackoverflow.com/a/47784205/3089840

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

So it sounds to me like the bindingsmiddleware is just a shortcut term for \Illuminate\Routing\Middleware\SubstituteBindings::class- If this is correct, I'm not sure why Laravel doesn't use the same terminology in both the weband the apiarrays in Kernel.php. It seems kind of inconsistent and confusing to use \Illuminate\Routing\Middleware\SubstituteBindings::classin the webarray and bindingsin the apiarray.

因此,它的声音,我像bindings中间件仅仅是一个捷径期限为\Illuminate\Routing\Middleware\SubstituteBindings::class-如果这是正确的,我不知道为什么Laravel不中均使用相同的术语webapi阵列中Kernel.php。这似乎有点矛盾和混淆使用\Illuminate\Routing\Middleware\SubstituteBindings::class了在web阵列bindings中的api阵列。

回答by Mladen Janjetovic

I think the thing that you're asking for is this https://laravel.com/docs/5.7/routing#route-model-binding

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

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

It binds the User model right away.

它立即绑定 User 模型。