symfony2 FOSRestBundle注释

use*_*980 9 symfony fosrestbundle

是否有人在控制器中使用put,get,post,delete注释(https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/Annotations/).

我试图像这样使用它,但它仍然需要获取方法.FOSRestBundle中那些注释的目的是什么?

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

Dat*_*aya 14

我想分享有关所有注释的信息.

@ Get,@ Post,@ Put,@ Delete,@ Head,@ Pick是@Route + @Method的快捷方式,而不是同时使用它们,你只需指定一个,例如:

    /**
     * @Get("/hello/{id}")
     * 
     */
    public function helloAction($id)
    {
        return array();
    }
Run Code Online (Sandbox Code Playgroud)

关于@View的信息在doc: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
Run Code Online (Sandbox Code Playgroud)

名称前缀可以添加到routing.yml文件中或作为注释添加.它还记录在案 - https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/6-automatic-route-generation_multiple-restful-controllers.md:

有时,路由自动命名将导致路由名称冲突,因此RestBundle路由集合提供name_prefix(xml/yml的名称前缀和注释的@NamePrefix)参数:

  #src/Acme/HelloBundle/Resources/config/users_routes.yml comments:
     type:         rest
     resource:     "@AcmeHelloBundle\Controller\CommentsController"
     name_prefix:  api_
Run Code Online (Sandbox Code Playgroud)

使用此配置,路由名称将变为:api_vote_user_comment

当您拥有父资源并且需要在子级1之前添加前缀时,@ Prefix特别有用.例:

父:

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

儿童:

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

现在,动作getCommentAction对应于/ users/{slug}/comments/{id}路径.

使用@Prefix("some_prefix")生成的路径将是/ users/{slug}/some_prefix/comments/{id}

通过使用@NoRoute方法级别注释,将不会生成路由.