使用Symfony2和FOSRestBundle我試圖實現API方法,這些API方法在路由中定義了一些固定參數以及查詢字符串中可能存在的一些可選參數。使用Symfony中的FOSRestBundle混合路由和查詢參數
例如:
http://somesite.com/api/method/a/b
http://somesite.com/api/method/c/d?x=1&y=2
據the documentation for FOSRestBundle,ParamFetcher是要做到這一點,利用@QueryParam標註的正確方法。不過,我已經有一個控制器,定義如下:
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的實例,但我不知道如何(以及谷歌搜索並沒有幫助太多) 。我從文檔中知道,我可以簡單地改變方法簽名來併入ParamFetcher,但是,當我這樣做時,它將參數移動到查詢字符串中,這是我不能擁有的。
是否有混合兩者的方法,還是應該放棄ParamFetcher並直接使用Symfomy的內置Request對象直接檢查請求?
感謝您的跟進 - 我終於弄清楚了這一點,並忽略了回來。 – futureal