Search code examples
restsymfonyfosrestbundle

Why symfony FOSRest bundle doesn't find the right controller?


I have two actions:

/**
 * @Rest\Get("/items/{itemId}")
 */
public function getAction(UuidInterface $id): View

And

/**
 * @Rest\Get("/items/available")
 */
public function getAvailableAction() : View

The thing is that when I'm trying to call getAvailableAction by a link items/available, the getAction is being called. I guess it interprets the word available as an {itemId} somewhy.

How should I solve it?


Solution

  • You guessed right. Just define a proper requirement:

     /**
      * @Rest\Get("/items/{itemId}", requirements={"itemId" = "\d+"})
      */
    

    If your itemId is an UUID, change the number regex from \d+ to [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12} or a simplified [a-fA-F0-9\-]{36}.

    Don't forget to clear the cache.