如何根据过滤器参数获取资源:
例如: ... / api / book?author = X
如果一个完整的例子,我会很高兴!
答案 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侦听器。