Laravel 5.5路由中的模型绑定不起作用 [英] Laravel 5.5 Model binding in routes doesn't work

查看:452
本文介绍了Laravel 5.5路由中的模型绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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 your web middleware group in your app/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.3文档-升级-中间件-绑定替代中间件

这篇关于Laravel 5.5路由中的模型绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆