【问题标题】:Laravel 8 custom routes return 404 while using resourceLaravel 8 自定义路由在使用资源时返回 404
【发布时间】:2021-09-04 12:53:19
【问题描述】:

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);

为什么会出现这个问题?并且资源路由放在最后可以吗?

【问题讨论】:

标签: laravel laravel-8 laravel-routing


【解决方案1】:

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

总结:

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

GET   /clients/{client}   show   clients.show

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

解决方案:

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

【讨论】:

    【解决方案2】:

    第一: 您需要将请求从 Routes/web.php 定向到 API

    Route::get('/{any}', [App\Http\Controllers\ClientController::class, 'index'])->where('any','.*');
    

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

    【讨论】:

      猜你喜欢
      • 2021-07-13
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 2017-05-22
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多