2012-11-15 28 views
1

使用FOSRestBundle驗證POST數據到Symfony2 Rest API的最佳方式是什麼?我通常會使用路由要求,但是通過FOSRestBundle自動生成路由,我不確定最好的方式去實現它嗎?使用Symfony2驗證數據FOSRestBundle

回答

3

如果您使用Param註釋,可以驗證POST數據的方式與GET數據的相同方式。因此,舉例來說,如果你需要一個ID和密碼,你可以使用:

/** 
* POST /login.json 
* 
* @RequestParam(name="id", requirements="\d+", description="User id") 
* @RequestParam(name="password", requirements="\w{8,}", description="Password") 
*/ 
public function postLoginAction(ParamFetcherInterface $paramFetcher) 
{ 
    ... 
} 

的使用注意事項@RequestParam而不是@QueryParam - 前者是POST,後者是GET。如果您需要更多關於使用Param註釋的示例,請查看示例應用程序。

+0

對!這個答案應該被接受,這正是我正在尋找的,我的谷歌搜索正是你的問題的標題。 – chalasr