Laravel将放置/修补路线定义为相同的路线名称 [英] Laravel define a put/patch route as the same route name

查看:95
本文介绍了Laravel将放置/修补路线定义为相同的路线名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中,使用路由资源快速生成路由负载非常方便:

In Laravel it's quite handy to quickly generate a load of routes by using a route resource:

Route::resource('things'ThingsController');

这将为CRUD操作生成所有必需的RESTful路由.其中之一是PUT/PATCH路由,其定义如下:

This will produce all the necessary RESTful routes for CRUD operations. One of these is the PUT/PATCH route which might be defined as follows:

PUT/PATCH things/{id} ThingsController@update things.update

我已经了解到,最好显式定义每个路由而不是使用路由资源,但是我将如何定义上面的PUT/PATCH路由.我知道我可以做到

I've read around that it's better to explicitly define each of your routes rather than use a route resource but how would I define the PUT/PATCH route above. I understand that I can do

Route::put('thing/{id}', ['as' => 'things.update']);

Route::patch('thing/{id}', ['as' => 'things.update']);

但是第二个将覆盖或与第一个允许things.update路由名称仅引用PUT或PATCH请求冲突.如何显式创建资源路由创建的组合PUT/PATCH路由?

But the second would overwrite or conflict with the first allowing the things.update route name to only refer to either a PUT or PATCH request. How can I explicitly create the combined PUT/PATCH route as created by the resource route?

推荐答案

繁琐的搜索之后,请尝试以下操作;

After tedious searching, try the following;

Route::match(array('PUT', 'PATCH'), "/things/{id}", array(
      'uses' => 'ThingsController@update',
      'as' => 'things.update'
));

这使您可以通过动词数组来限制请求.

This allows you to restrict request via an array of Verbs.

或者您可以这样限制资源;

Or you can limit the resource as so;

Route::resource('things', 'ThingsController',
        array(
           'only' => array('update'), 
           'names' => array('update' => 'things.update')
        ));

两者都应提供相同的结果,但请注意,它们未经测试.

Both should provide the same result, but please note they are not tested.

这篇关于Laravel将放置/修补路线定义为相同的路线名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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