[英]Laravel API Routes Return 404
我正在使用Laravel 5.6,并且在route / api.php中的现有路由上获取HTTP404响应,其定义如下:
Route::middleware('auth:api')->post('/account/plan', 'Account\BillingController@updatePlan');
Route::middleware('auth:api')->put('/account/plan', 'Account\BillingController@unsubscribe');
Route::middleware('auth:api')->patch('/account/plan', 'Account\BillingController@resubscribe');
当我在这些路由上使用axios.post()并包含_method参数时,我在PUT和PATCH路由上收到404响应。 我还测试了axios.put()/ axios.patch(),以在不使用_method参数的情况下使用post()来代替。 我还确认这些是工匠route:list正确代表的:
| | POST | api/account/plan | | App\Http\Controllers\Account\BillingController@updatePlan | api,auth:api |
| | PUT | api/account/plan | | App\Http\Controllers\Account\BillingController@unsubscribe | api,auth:api |
| | PATCH | api/account/plan | | App\Http\Controllers\Account\BillingController@resubscribe | api,auth:api |
Axios呼叫示例:
axios.post(url,{_method:"PUT",confirm:"unsubscribe"})
.then(response => callback(response.data))
.catch(error => console.log(error))
当我按以下方式定义这些相同的路线时,它们都按预期工作:
Route::middleware('auth:api')->post('/account/plan', 'Account\BillingController@updatePlan');
Route::middleware('auth:api')->post('/account/unsubscribe', 'Account\BillingController@unsubscribe');
Route::middleware('auth:api')->post('/account/resubscribe', 'Account\BillingController@resubscribe');
我可以通过其他路由上的请求方法将端点分开,但我不确定为什么会造成问题。 有人可以解释为什么我得到404响应以及如何避免它们吗?
既然您似乎做得不错,也许可以通过遵循laravel更严格的约定来定义路线,您不会遇到问题吗? 尝试这样:
Route::middleware(['auth:api'])->group(function () {
Route::post('/account/plan', 'Account\BillingController@updatePlan');
Route::put('/account/plan', 'Account\BillingController@unsubscribe');
Route::patch('/account/plan', 'Account\BillingController@resubscribe');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.