Symfony2 REST包:筛选参数

时间:2014-04-24 08:18:16

标签: api rest symfony

如何根据过滤器参数获取资源:

例如: ... / api / book?author = X

如果一个完整的例子,我会很高兴!

1 个答案:

答案 0 :(得分:6)

根据FOSRestBundle,您可以使用QueryParam注释和ParamFetcher,如下所示,

use FOS\RestBundle\Controller\FOSRestController;
// ...
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\Annotations\QueryParam;

class BookController extends FOSRestController
{
    /**
     *
     * @QueryParam(name="author", description="the author of the book")
     */
    public function getBookAction(ParamFetcher $paramFetcher)
    {
         $author = $paramFetcher->get('author'); // Can then be used to filter books on author

         // do something ...
    }

<强>要求,

  • 您必须在param_fetcher_listener: true文件的捆绑包配置中添加app/config/config.yml来启用param fetcher侦听器。