Search code examples
apisymfonyparametersfosrestbundlenelmioapidocbundle

How to pass multiple parameter in a GET request inside a API symfony2?


I am using a FOSRestBundle and I am pretty new in Symfony.

I've been following some exemples I've seen but I can't add more than one parameter in the request.

For example

/**
     *  
     * @param  string $id Identifier
     * @return [type]     [description]
     *
     * @ApiDoc()
     */
    public function getProductsAction($id)
    {
        return myfunction($id);
    }

works fine, but if I want to do a variation like this:

 /**
     *  
     * @param  string $id Identifier
     * @return [type]     [description]
     *
     * @ApiDoc()
     */
    public function getProductsAction($id, $month)
    {
        return myfunction($id, $month);
    }

It doesn't work. It identifies only the $id

There is any limitation I am not aware? or some extra configuration beyond the simple modification I did?


Solution

  • When you write

    /**
     *  
     * @param  string $id Identifier
     * @return [type]     [description]
     *
     * @ApiDoc()
     */
    public function getProductsAction($id)
    {
        return myfunction($id);
    }
    

    it's equivalent to

    use FOS\RestBundle\Controller\Annotations\Get;
    
    /**
     * @Get("/products/{id}")
     * @param  string $id Identifier
     * @return [type]     [description]
     *
     * @ApiDoc()
     */
    public function getProductsAction($id)
    {
        return myfunction($id);
    }
    

    Symfony maps automatically the route and parameter.

    But when you write

    /**
     *  
     * @param  string $id Identifier
     * @return [type]     [description]
     *
     * @ApiDoc()
     */
    public function getProductsAction($id, $month)
    {
        return myfunction($id, $month);
    }
    

    Symfoy doesn't know what to do with your $month parameter, you need to tell it

    use FOS\RestBundle\Controller\Annotations\Get;
    
    /**
     * @Get("/products/{id}/{month}")
     * @param  string $id Identifier
     * @return [type]     [description]
     *
     * @ApiDoc()
     */
    public function getProductsAction($id, $month)
    {
        return myfunction($id, $month);
    }