I am coding an API to improve my personnal PHP skills. I have a problem with 2 routes :
recette.recetteById:
path: /api/recette/{id}
controller: App\Controller\RecetteController::recetteById
recette.subRecette:
path: /api/recette/sub-recette
controller: App\Controller\RecetteController::subRecette
When I go to /api/recette/sub-recette
it shows me the return of my recetteById
function instead of subRecette
, it seems that /sub-recette
is used as /{id}
.
My recetteById
is just a function that returns the id as json, so when I go to /api/recette/sub-recette
it show sub-recette
. Why the route doesn't call my subRecette
function?
What do I have to do to call subRecette
function when I go /api/recette/sub-recette
recetteByID :
public function recetteById($id) {
return $this->json($id);
}
CodePudding user response:
After editing the question and reading it more closely, I see what the issue is.
The routing works by the principle first match wins. To it, the sub-recette
looks a lot like a perfectly valid ID.
In order to circumvent this, if your ID is, say, number, you could do:
recette.recetteById:
path: /api/recette/{id}
requirements:
id: '\d '
controller: App\Controller\RecetteController::recetteById
Another way to achieve this is to put subRecette
before recetteById
but this is error-prone.