Laravel 5.5路由中的模型绑定不起作用 [英] Laravel 5.5 Model binding in routes doesn't work
问题描述
在我的routes.php
中,我有这个:
Route::get('user/{user}/permissions/','UserController@permissions')->name('user.permissions');
在我的控制器中,我有:
In my controller I have:
public function permissions(User $user){
dd($user);
}
$ user 是空对象(如新用户; 无属性)
$user is empty object (like new user; without attributes)
如果我使用:
public function permissions($user){
dd(User::find($user));
}
完美工作!
我以前有Laravel 5.2,并且此代码可以正常工作,但是在Laravel 5.5中它不起作用,为什么有任何想法?
I have previously Laravel 5.2 and this code works fine but in Laravel 5.5 it doesn't work, any ideas why?
推荐答案
您的声音从5.2升级到了某些版本.
Sounds like you upgraded from 5.2 up to ... some version.
Laravel 5.3使用SubstitueBindings
中间件进行隐式和显式绑定,不再在中间件堆栈之前通过路由器完成
Laravel 5.3 uses the SubstitueBindings
middleware to do the implicit and explicit bindings, it is no longer done via the router before the middleware stack.
如果您升级了该中间件并且未将其添加到任何组中,则将没有路由模型绑定,因为中间件负责用绑定替换参数.
If you upgraded and did not add this middleware to any of the groups, you will not have your route model bindings as the middleware is responsible for substituting the parameter with the binding.
路由模型绑定现在已使用中间件完成.所有应用程序都应将
Illuminate\Routing\Middleware\SubstituteBindings
添加到app/Http/Kernel.php
文件中的web
中间件组中:
"Route model binding is now accomplished using middleware. All applications should add the
Illuminate\Routing\Middleware\SubstituteBindings
to yourweb
middleware group in yourapp/Http/Kernel.php
file:
\Illuminate\Routing\Middleware\SubstituteBindings::class,
您还应该在HTTP内核的$routeMiddleware
属性中注册用于绑定替换的路由中间件:
You should also register a route middleware for binding substitution in the $routeMiddleware
property of your HTTP kernel:
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
..."
这篇关于Laravel 5.5路由中的模型绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!