Search code examples
laravellaravel-8laravel-routing

Laravel 8 custom routes return 404 while using resource


The API endpoint /clients/entries returns a 404 error while using the route setup.

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

The endpoint only works when they are rearranged, so the resource route is at the end.

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

Why does this issue occur? And is it fine to have the resource route at the end?


Solution

  • A full explanation can be found at https://stackoverflow.com/a/62952620/9004987 (thanks to @Espresso).

    Summary:

    When the resource route is registered at the beginning it will create some routes. Example:

    GET   /clients/{client}   show   clients.show
    

    And when other custom routes (such as /clients/entries) are registered after the resource route then it will conflict with the resource URI (since it has the same pattern).

    Solution:

    Define the custom routes before the resource route.