Laravel API 中的可选参数 - 请求被错误感知
Laravel optional parameter in API-request perceived incorrectly
在我的 api.php 我有这些路线。
我可以请求端点:/api/v2/commodities/1
- 这个请求被重定向到 APICommodityController@read
- 没问题!
每当我 GET 请求 /v2/commodities/categories/
时就会出现问题。我希望将此请求重定向到路由组:'categories',但 Laravel 将其视为第一组中的可选参数(~ 代码中的 "HERE"评论)。
我该如何解决这个问题?
Route::group( [ 'prefix' => 'v2' ], function() {
/**
* Commodity
*/
Route::group( [ 'prefix' => 'commodities' ], function() {
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* R: READ (HERE)
*/
Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\Commodity\APICommodityController@delete' );
/**
* Commodity categories
*/
Route::group( [ 'prefix' => 'categories' ], function() {
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* R: READ
*/
Route::get( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@delete' );
} );
} );
在您的 id 参数末尾添加此条件:
->where('id', '[0-9]+')
所以它变成了这个样子:
Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' )->where('id', '[0-9]+');
只需将商品类别路线部分移至所有其他路线定义之上即可。这将使 laravel 注册此静态路由而不会被动态路由捕获。
类似的东西:
Route::group( [ 'prefix' => 'v2' ], function() {
/**
* Commodity
*/
Route::group( [ 'prefix' => 'commodities' ], function() {
/**
* Commodity categories
*/
Route::group( [ 'prefix' => 'categories' ], function() {
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* R: READ
*/
Route::get( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@delete' );
} );
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* R: READ (HERE)
*/
Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\Commodity\APICommodityController@delete' );
} );
在我的 api.php 我有这些路线。
我可以请求端点:/api/v2/commodities/1
- 这个请求被重定向到 APICommodityController@read
- 没问题!
每当我 GET 请求 /v2/commodities/categories/
时就会出现问题。我希望将此请求重定向到路由组:'categories',但 Laravel 将其视为第一组中的可选参数(~ 代码中的 "HERE"评论)。
我该如何解决这个问题?
Route::group( [ 'prefix' => 'v2' ], function() {
/**
* Commodity
*/
Route::group( [ 'prefix' => 'commodities' ], function() {
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* R: READ (HERE)
*/
Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\Commodity\APICommodityController@delete' );
/**
* Commodity categories
*/
Route::group( [ 'prefix' => 'categories' ], function() {
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* R: READ
*/
Route::get( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@delete' );
} );
} );
在您的 id 参数末尾添加此条件:
->where('id', '[0-9]+')
所以它变成了这个样子:
Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' )->where('id', '[0-9]+');
只需将商品类别路线部分移至所有其他路线定义之上即可。这将使 laravel 注册此静态路由而不会被动态路由捕获。
类似的东西:
Route::group( [ 'prefix' => 'v2' ], function() {
/**
* Commodity
*/
Route::group( [ 'prefix' => 'commodities' ], function() {
/**
* Commodity categories
*/
Route::group( [ 'prefix' => 'categories' ], function() {
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@updateOrCreate' );
/**
* R: READ
*/
Route::get( '{id?}', 'API\v2\CommodityCategories\APICommodityCategoriesController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\CommodityCategories\APICommodityCategoriesController@delete' );
} );
/**
* C: CREATE
*/
Route::post( '{id?}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* U: UPDATE
*/
Route::put( '{id}', 'API\v2\Commodity\APICommodityController@updateOrCreate' );
/**
* R: READ (HERE)
*/
Route::get( '{id?}', 'API\v2\Commodity\APICommodityController@read' );
/**
* D: DELETE
*/
Route::delete( '{id}', 'API\v2\Commodity\APICommodityController@delete' );
} );