使用 FOSRestBundle 和 Symfony 混合路由和查询参数 [英] Mixing route and query parameters using FOSRestBundle with Symfony

查看:31
本文介绍了使用 FOSRestBundle 和 Symfony 混合路由和查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Symfony2 和 FOSRestBundle 我试图实现 API 方法,这些方法在路由中定义了一些固定参数以及查询字符串中可能存在的一些可选参数.

Using Symfony2 and FOSRestBundle I am attempting to implement API methods that have some number of fixed parameters defined in the route along with some optional parameters that may exist in the query string.

例如:

 http://somesite.com/api/method/a/b
 http://somesite.com/api/method/c/d?x=1&y=2

根据 FOSRestBundle 的文档,ParamFetcher 是使用@QueryParam 注释执行此操作的正确方法.但是,我已经定义了一个控制器,如:

According to the documentation for FOSRestBundle, ParamFetcher is the proper way to do this, using the @QueryParam annotation. However, I already have a controller defined like:

 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use FOS\RestBundle\Controller\Annotations\Get;
 use FOS\RestBundle\Controller\Annotations\View;

 class MyController extends Controller
 {

   /**
    * @Get("/method/{a}/{b}")
    * @View()
    */
   public function getMethodAction($a, $b)
   {
     // do stuff

     return array('foo' => 'bar');
   }

 }

现在看来我需要能够访问 ParamFetcher 的实例,但我不知道如何(而且 Google 搜索并没有多大帮助).我从文档中知道我可以简单地更改方法签名以合并 ParamFetcher,但是,当我这样做时,它会将参数移动到我不能拥有的查询字符串中.

Now it seems I need to be able to get access to an instance of ParamFetcher, but I don't know how (and Google searches have not helped much). I know from the documentation that I can simply change the method signature to incorporate ParamFetcher, however, when I do that it moves the parameters into the query string, which I can't have.

有没有办法将两者混合,或者我应该放弃 ParamFetcher 并直接使用 Symfomy 的内置 Request 对象检查请求?

Is there a way to mix the two, or should I give up on ParamFetcher and go to just inspecting the request directly using Symfomy's built-in Request object?

推荐答案

这个问题很老了,您可能已经找到了解决方案,但自从我通过 Google 搜索来到这里并知道答案后,我会做出贡献.

This question is quite old and you probably found a solution already but since I got here through Google search and know an answer I will contribute.

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;

class DefaultController extends Controller
{
    /**
     * Returns a collection of Task
     *
     * @QueryParam(name="projectId", nullable=true, requirements="\d+")
     * @QueryParam(name="name", nullable=true, description="Project Name")
     * @QueryParam(name="assignee", nullable=true)
     * @QueryParam(name="depth", nullable=true)
     *         *
     * @param ParamFetcher $paramFetcher
     * @ApiDoc()
     *
     * @return JsonResponse
     */
    public function cgetTaskAction(ParamFetcher $paramFetcher)
    {
        foreach ($paramFetcher->all() as $criterionName => $criterionValue) {
            // some logic here, eg building query
        }

        $results = // query database using criteria from above

        // this is just a simple example how to return data
        return new JsonResponse($results);
    }
}

这篇关于使用 FOSRestBundle 和 Symfony 混合路由和查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆