2011-10-24 3 views
9

컨트롤러에 주석 (https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/Annotations/)을 넣거나, 가져오고, 게시하고, 삭제하는 데 사용되는 사람이 있습니까?symfony2 FOSRestBundle annotations

나는 이것을 이렇게 사용하려고 노력하고 있지만 여전히 방법을 취합니다. FOSRestBundle

/** 
* @Route("/get/{id}", defaults={"_format" = "json"}) 
* @Post 
*/ 
public function getObject($id) {  
    $object = $this->getService()->findById($id); 
    return $object; 
} 

답변

13

모든 주석에 대한 정보를 공유하고 싶습니다.

@Get, @Post, @Put, @Delete, @Head, @Patch 대신, 당신은 단지 하나를 지정할 수 있습니다 그들 모두를 사용하여, @Route + @Method에 대한 바로 가기입니다 예 :

/** 
    * @Get("/hello/{id}") 
    * 
    */ 
    public function helloAction($id) 
    { 
     return array(); 
    } 

정보에 대한 @View는 문서에 있습니다 https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md

@View //Guess template name 
@View("AcmeHelloBundle::layout.html.twig") //Load Resources/views/layout.html.twig 
@View("AcmeHelloBundle::layout.html.twig", templateVar="test") // if returned data doesn't 
    // have a key (e.g. return array("string", 5) instead of default variable 'data', 
    // it's placed inside 'test' variable inside template. 
@View(statusCode=204) // set HTTP header's status code 

이름 접두사이 routing.yml 파일 또는 주석으로 하나를 추가 할 수 있습니다. 또한 설명되어 있습니다 - https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/6-automatic-route-generation_multiple-restful-controllers.md :

때때로, 라우팅 자동으로 이름이 너무 RestBundle 경로 컬렉션 매개 변수 (주석 XML/YML 및 @NamePrefix에 대한 이름 접두사)를 name_prefix을 제공, 경로 이름 충돌로 이어질 것 :

이 구성
#src/Acme/HelloBundle/Resources/config/users_routes.yml comments: 
    type:   rest 
    resource:  "@AcmeHelloBundle\Controller\CommentsController" 
    name_prefix: api_ 

는, 경로 이름이 될 것입니다 : api_vote_user_comment

@Prefix은 부모 리소스가 있고 하위 리소스 전에 접두사를 추가해야하는 경우 특히 유용합니다. 예 :

부모 :

class UsersController extends Controller 
{ 
    public function getUserAction($slug) 
    {} // "get_user" [GET] /users/{slug} 
} 

아이 :

class CommentsController extends Controller 
{ 
    public function getCommentAction($slug, $id) 
    {} // "get_user_comment" [GET] 
} 

지금 액션 getCommentAction이 /사용자/{슬러그}에 해당하는/댓글/{ID} 경로.

@prefix 함께

("some_prefix") 발생 경로 것이다/사용자/{슬러그}/some_prefix/의견/{ID}

그리고 @NoRoute 메소드 레벨의 주석을 사용하여

, 경로 생성되지 않습니다.

2

에서 그 주석의 목적은 (즉, GET과 동일이기 때문에) 당신은 경로에 ID를 넣으면 안됩니다 무엇입니까. 대신 id 매개 변수가 $ _POST를 통해 전송되게하려면

/** 
* @Route("/get", defaults={"_format" = "json"}) 
* @Post 
*/ 
public function getObject() { 
    $id = $this->Request::createFromGlobals()->request->get('id'); 
    $object = $this->getService()->findById($id); 
    return $object; 
}