Question

I'm using Symfony 2.3.4 and FOSRestBundle 0.13.1. I have configured the routes to be auto-generated by the FOSRestBundle. Eveything works great until I add the @QueryParam annotation to any method. This annotation changes the route and instead of extracting the varaible from the url it expects it to be passed as a parameter.

i.e

/**
* @return array
* @Rest\View
*/
public function getDetailsAction($user) {
......
}

-bash-4.2$ php app/console router:debug 
get_details      GET    ANY    ANY  /api/users/details/{user}

But as soon as I add the @QueryParam annotation, my route changes to:

/**
* @QueryParam(name="user", requirements="\w+", strict=true, nullable=false, description="Name of the user to query details for")
* @return array
* @Rest\View
*/
public function getDetailsAction($user) {
......
}

-bash-4.2$ php app/console router:debug 
get_details      GET    ANY    ANY  /api/users/details

Why are my routes changing? Is it not possible to keep the original routes and use the @QueryParam annotation at the same time?

Was it helpful?

Solution

By using the QueryParam annotation, you declare that the $user parameter is supplied as a query parameter (like /api/user/details?user=foo), so there are no more action parameters left to include in the route.

If you want to add requirements to the auto-generated route, you have to use the @Rest\Route annotation instead, as in

/**
 * @Rest\View
 * @Rest\Route(requirements={"user"="\w+"})
 */
public function getDetailsAction($user) {
-----
}

Note that there is no equivalent to the strict or nullable options. If the requirements aren't fulfilled or the parameter is missing, the route simply won't match, which leads to a 404 error if no other route matches the request. Also, AFAIK there is no way to attach a description to route parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top