Laravel 8自定义路由在使用资源时返回404



API端点/clients/entries在使用路由设置时返回404错误。

Route::apiResource('clients', ClientController::class);
Route::get('clients/entries', [ClientController::class , 'getAll']);

端点只有在重新排列时才起作用,因此资源路由在最后。

Route::get('clients/entries', [ClientController::class , 'getAll']);
Route::apiResource('clients', ClientController::class);

为什么会出现此问题?最后有资源路线可以吗?

完整的解释可以在https://stackoverflow.com/a/62952620/9004987(感谢@Espresso(。

摘要:

当资源路由在一开始注册时,它将创建一些路由。示例:

GET   /clients/{client}   show   clients.show

当其他自定义路由(如/clients/entries(在资源路由之后注册时,它将与资源URI冲突(因为它具有相同的模式(。

解决方案:

在资源路由之前定义自定义路由。

冷杉:您需要将请求从Routes/web.php直接发送到API

Route::get('/{any}', [AppHttpControllersClientController::class, 'index'])->where('any','.*');

需要回答来自routes/api.php的请求

最新更新