FOSRestBundle: routes and annotations for parameters

Roman Newaza picture Roman Newaza · Nov 23, 2013 · Viewed 14.8k times · Source

I'm able to get GET parameters with @QueryParam() annotation, but it looks like it works for Query String data only: /user?id=123.

I'd prefer to have it like /user/123 instead. For this, I might use @Get("/user/{id}") annotation, but I don't see it has additional metadata which @QueryParam() has:

name="id", requirements="\d+", default="1", description="User id"

If I use both of the annotations, I get an error:

ParamFetcher parameter conflicts with a path parameter 'id' for route 'getone'

My conflicting docblock:

/**
 * Finds and displays a Users entity.
 *
 * @Rest\View
 * @Rest\Get("/user/{id}")
 * @Rest\QueryParam(name="id", requirements="\d+", default="1", description="User id")
 * @ApiDoc(section="Partner Users")
 * @param int $id
 * @return array
 */

PS I need to have an id in the path (/user/123), not in query, and I also need to use @QueryParam() as it's read by NelmioApiDocBundle. How may I resolve this issue?

Answer

Nicolai Fröhlich picture Nicolai Fröhlich · Nov 23, 2013

FOSRestBundle's @Get annotation extends FOSRestBundle's @Route which in turn extends SensioFrameworkExtraBundle's @Route.

Have a look at the code and see the documentation chapter @Route and @Method.

The requirements and defaults attributes expect an array.

/**
  * @Rest\View
  * @Rest\Get("/user/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})
  * @ApiDoc(
  *  description="Returns a User Object",
  *  parameters={
  *      {"name"="id", "dataType"="integer", "required"=true, "description"="User Id"}
  *  }
  * )
  */
 public function getAction($id)
 {
    // ...
 }