I'm building a mock-up API using the Laravel resource controllers all works well until I try to override the default Laravel resource route when calling a get create but Laravel simply wont accept the overwrite. The documentation states the following:
我正在使用Laravel资源控制器构建模型API一直运行良好,直到我在调用get create时尝试覆盖默认的Laravel资源路由,但Laravel根本不接受覆盖。文档说明如下:
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes:
如果有必要在资源控制器之外添加除默认资源路由之外的其他路由,则应在调用Route :: resource之前定义这些路由;否则,资源方法定义的路由可能会无意中优先于您的补充路由:
In my code I have the following:
在我的代码中,我有以下内容:
routes.php
routes.php文件
Route::group(['prefix' => 'api/v1', 'middleware' => 'cors'], function () {
Route::get('test/create', 'V1\Universal\TestController@create2');
Route::resource('test', 'V1\Universal\TestController');
});
TestController.php
TestController.php
public function create2()
{
return "create 2 override function";
}
public function create()
{
return "create default function";
}
When calling the API /api/v1/test/create its always firing the create() method rather than the create2() method. According to the Laravel documentation my custom additional route should take precedence as its defined before the resource.
在调用API / api / v1 / test / create时,它总是触发create()方法而不是create2()方法。根据Laravel文档,我的自定义附加路径应优先于资源之前定义的路径。
Any Ideas?
有任何想法吗?
1 个解决方案
#1
1
If you want to overrite resource routes, you want to try to add them after Route::resource
. The quote from documentation tells about how to add routes to resource route, not about how to override them:
如果要覆盖资源路由,则需要尝试在Route :: resource之后添加它们。文档中的引用说明了如何将路由添加到资源路由,而不是如何覆盖它们:
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes
如果有必要在资源控制器之外添加除默认资源路由之外的其他路由,则应在调用Route :: resource之前定义这些路由;否则,资源方法定义的路由可能会无意中优先于您的补充路由
You may also use except
argument:
您也可以使用except参数:
Route::group(['prefix' => 'api/v1', 'middleware' => 'cors'], function () {
Route::get('test/create', 'V1\Universal\TestController@create2');
Route::resource('test', 'V1\Universal\TestController', ['except' => ['create']]););
});
In this case Route::resource
will not create create
route.
在这种情况下,Route :: resource不会创建创建路由。
#1
1
If you want to overrite resource routes, you want to try to add them after Route::resource
. The quote from documentation tells about how to add routes to resource route, not about how to override them:
如果要覆盖资源路由,则需要尝试在Route :: resource之后添加它们。文档中的引用说明了如何将路由添加到资源路由,而不是如何覆盖它们:
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes
如果有必要在资源控制器之外添加除默认资源路由之外的其他路由,则应在调用Route :: resource之前定义这些路由;否则,资源方法定义的路由可能会无意中优先于您的补充路由
You may also use except
argument:
您也可以使用except参数:
Route::group(['prefix' => 'api/v1', 'middleware' => 'cors'], function () {
Route::get('test/create', 'V1\Universal\TestController@create2');
Route::resource('test', 'V1\Universal\TestController', ['except' => ['create']]););
});
In this case Route::resource
will not create create
route.
在这种情况下,Route :: resource不会创建创建路由。