Laravel 5路由没有定义,而它是? [英] Laravel 5 route not defined, while it is?

查看:77
本文介绍了Laravel 5路由没有定义,而它是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对应该如何工作有些困惑.但是我出现了Route [/preferences/1] not defined错误.

在我的route.php中,我有:

Route::patch('/preferences/{id}', 'UserController@update');

在查看文件(account/preferences.blade.php)中,我有:

{!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => '/preferences/' . Auth::user()->id]) !!}

我收到一条错误消息,告诉我该路线不存在.我认为我对该主题的文档有误解,但我认为我已经为带有给定参数的PATCH请求定义了一条路由,并在视图中正确设置了此路径.

我在这里俯瞰什么?

解决方案

在表单打开中执行['route' => 'someroute']时将调用route()方法,该方法需要称为命名路径的 .您给路由指定这样的名称:

Route::patch('/preferences/{id}',[
    'as' => 'user.preferences.update',
    'uses' => 'UserController@update'
]);

也就是说,将路由的第二个参数放入数组,在其中指定路由名称(as),以及在命中路由时要执行的操作(uses). /p>

然后,当您打开表单时,调用该路线:

{!! Form::model(Auth::user(), [
    'method' => 'PATCH',
    'route' => ['user.preferences.update', Auth::user()->id]
]) !!}

现在,对于没有参数的路由,您可以执行'route' => 'routename',但是由于有参数,因此可以创建一个数组并按顺序提供参数.

所有这些,由于您似乎正在更新当前用户的首选项,因此我建议您让处理控制器检查当前登录用户的ID,并以此为基础进行更新-无需发送除非您的用户还需要更新其他用户的偏好设置,否则请在url和路由中的ID中输入. :)

I'm a little confused on how this is supposed to work. But I'm getting an Route [/preferences/1] not defined error.

In my routes.php I have:

Route::patch('/preferences/{id}', 'UserController@update');

And in the view file (account/preferences.blade.php) I have:

{!! Form::model(Auth::user(), ['method' => 'PATCH', 'route' => '/preferences/' . Auth::user()->id]) !!}

I'm getting an error telling me the route doesn't exist. I think I'm misunderstanding the docs on this topic but in my opinion I've defined a route for PATCH requests with a given parameter, and set this in the view correctly.

What am I overlooking here?

解决方案

The route() method, which is called when you do ['route' => 'someroute'] in a form opening, wants what's called a named route. You give a route a name like this:

Route::patch('/preferences/{id}',[
    'as' => 'user.preferences.update',
    'uses' => 'UserController@update'
]);

That is, you make the second argument of the route into an array, where you specify both the route name (the as), and also what to do when the route is hit (the uses).

Then, when you open the form, you call the route:

{!! Form::model(Auth::user(), [
    'method' => 'PATCH',
    'route' => ['user.preferences.update', Auth::user()->id]
]) !!}

Now, for a route without parameters, you could just do 'route' => 'routename', but since you have a parameter, you make an array instead and supply the parameters in order.

All that said, since you appear to be updating the current user's preferences, I would advise you to let the handling controller check the id of the currently logged-in user, and base the updating on that - there's no need to send in the id in the url and the route unless your users should need to update the preferences of other users as well. :)

这篇关于Laravel 5路由没有定义,而它是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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