FOSRestBundle: Routes and Parameter Annotations

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

I would prefer it to be like /user/123 . I could use the @Get("/user/{id}") annotation @Get("/user/{id}") , but I don’t see that it has additional metadata that @QueryParam() has:

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

If I use both annotations, I get an error:

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

My conflicting dock block:

 /** * 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 the identifier in the path ( /user/123 ) and not in the request, and I also need to use @QueryParam() as it is read by NelmioApiDocBundle. How can I solve this problem?

+7
symfony fosrestbundle
source share
2 answers

FOSRestBundle @Get annotation extends FOSRestBundle @Route , which in turn extends SensioFrameworkExtraBundle @Route .

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

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) { // ... } 
+16
source share

If you want the requirements description to just do this in your annotation

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

All Articles