“绑定”中间件在 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
What does "bindings" middleware do in Laravel 5.6?
提问by Inigo
Just as per the title. Default api middleware in Laravel 5.6 is listed in Kernel.php
as:
就像标题一样。Laravel 5.6 中默认的 api 中间件Kernel.php
如下所示:
protected $middlewareGroups = [
'api' => [
'throttle:60,1',
'bindings',
],
];
I'd appreciate a layman's explanation of what bindings
does, which I can't find anywhere.
我很感激外行对什么的解释bindings
,我在任何地方都找不到。
It uses the SubstituteBindings
class which has the handle
method:
它使用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 binding
is 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 bindings
middleware 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 web
and the api
arrays in Kernel.php
. It seems kind of inconsistent and confusing to use \Illuminate\Routing\Middleware\SubstituteBindings::class
in the web
array and bindings
in the api
array.
因此,它的声音,我像bindings
中间件仅仅是一个捷径期限为\Illuminate\Routing\Middleware\SubstituteBindings::class
-如果这是正确的,我不知道为什么Laravel不中均使用相同的术语web
和api
阵列中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 模型。