Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSRestBundle: routes and annotations for parameters

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?

like image 557
Roman Newaza Avatar asked Nov 23 '13 11:11

Roman Newaza


1 Answers

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)
 {
    // ...
 }
like image 145
Nicolai Fröhlich Avatar answered Nov 02 '22 21:11

Nicolai Fröhlich