【问题标题】:How to pass multiple parameter in a GET request inside a API symfony2?如何在 API symfony2 内的 GET 请求中传递多个参数?
【发布时间】:2017-04-10 00:06:15
【问题描述】:

我正在使用 FOSRestBundle,而且我是 Symfony 的新手。

我一直在关注一些我见过的例子,但我不能在请求中添加多个参数。

例如

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

工作正常,但如果我想做这样的变化:

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

它不起作用。它仅标识 $id

有什么我不知道的限制吗?还是我所做的简单修改之外的一些额外配置?

【问题讨论】:

    标签: api symfony parameters fosrestbundle nelmioapidocbundle


    【解决方案1】:

    当你写作时

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

    相当于

    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 自动映射路由和参数。

    但是当你写的时候

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

    Symfoy 不知道如何处理您的 $month 参数,您需要告诉它

    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);
    }
    

    【讨论】:

    • @get 是来自哪个 symfony 组件的注解?我收到了一个未导入的使用声明。
    • @MatheusOliveira 这是来自 FOSRestBundle 的注释,我已将其包含在答案中 :)
    猜你喜欢
    • 2020-09-11
    • 2020-08-20
    • 2020-02-25
    • 2013-03-12
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多