Ali Briceño Ali Briceño - 8 months ago 86
PHP Question

Symfony FOSRestBundle Actions Order

i get started a new proyect with symfony3 and FOSRestBundle for a REST API. Everything is working as expected, but i have a question. I have this two actions on my controller and the router config:

api_marcas_get_all_marca_paginated GET ANY ANY /api/marca/{limit}/{page}.{_format}

api_marcas_delete_remove_marca GET ANY ANY /api/marca/delete/{marcaid}.{_format}

The problem is that when i call this (from Postman):

The other action (api_marcas_get_all_marca_paginated) responds...

Yes.. i know that i can use the "DELETE" http method, and this is worked... but... Why do they collide?

Here are my actions:

* @Rest\Get("/marca/{limit}/{page}")
public function getAllMarcaPaginatedAction(Request $request)
{... code here ...}


* @Rest\Get("/marca/delete/{marcaid}")
public function deleteRemoveMarcaAction(Request $request)
{ ... //code here... }

On that order.

I am doing something wrong?
Has anything to do with order of the actions? Thanks...

Answer Source

It happens because those rules are considered sequentially, and the first match is taken.

In your example /api/marca/delete/105 the first rule matches as delete to be the {limit} and 105 to be the {page}.

You can define some requirements for the {limit} so to match only certain patterns. Here you can find some examples of how to use the requirements annotation:

On the other side, it's the recommended practice not to use GET method for operations that alter the state of the application (like deleting an item), being considered a safe method.