symfony2 FOSRestBundle 注释 [英] symfony2 FOSRestBundle annotations

查看:29
本文介绍了symfony2 FOSRestBundle 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人使用过在控制器中放置、获取、发布、删除注释(https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/Annotations/).

我正在尝试像这样使用它,但它仍然需要 get 方法.FOSRestBundle中那些Annotations的目的是什么

/*** @Route("/get/{id}", defaults={"_format" = "json"})* @邮政*/公共函数 getObject($id) {$object = $this->getService()->findById($id);返回 $object;}

解决方案

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

@Get、@Post、@Put、@Delete、@Head、@Patch 是@Route + @Method 的快捷方式,您可以指定一个,而不是同时使用它们,例如:

/*** @Get("/hello/{id}")**/公共函数 helloAction($id){返回数组();}

关于 @View 的信息在文档中:https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/3-listener-support.md

@View//猜测模板名称@View("AcmeHelloBundle::layout.html.twig")//加载资源/views/layout.html.twig@View("AcmeHelloBundle::layout.html.twig", templateVar="test")//如果返回的数据没有//有一个键(例如 return array("string", 5) 而不是默认变量 'data',//它被放置在模板内的 'test' 变量中.@View(statusCode=204)//设置 HTTP 头的状态码

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

<块引用>

有时,路由自动命名会导致路由名称冲突,所以RestBundle 路由集合提供了一个 name_prefix (name-prefix forxml/yml 和 @NamePrefix 用于注释)参数:

 #src/Acme/HelloBundle/Resources/config/users_routes.yml 注释:类型:休息资源:@AcmeHelloBundle\Controller\CommentsController"名称前缀:api_

<块引用>

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

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

父母:

class UsersController 扩展控制器{公共函数 getUserAction($slug){}//"get_user" [GET]/users/{slug}}

孩子:

class CommentsController 扩展控制器{公共函数 getCommentAction($slug, $id){}//"get_user_comment" [GET]}

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

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

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

Is anyone used put, get, post, delete annotations(https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/Annotations/) in controller.

I'm trying to use it like this, but it still takes get methods. What is the purpose of those Annotations in FOSRestBundle

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

解决方案

I want to share info about all annotations.

@Get, @Post, @Put, @Delete, @Head, @Patch are shortcuts for @Route + @Method, instead of using them both, you can just specify one, e.g.:

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

Info about @View is in 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

Name prefix can be added either to routing.yml file or as a annotation. It is also documented - https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/6-automatic-route-generation_multiple-restful-controllers.md :

Sometimes, routes auto-naming will lead to route names collisions, so RestBundle route collections provides a name_prefix (name-prefix for xml/yml and @NamePrefix for annotations) parameter:

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

With this configuration, route name would become: api_vote_user_comment

@Prefix is especially useful when you have parent resource and need to add prefix before child one. Example:

parent:

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

child:

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

Now the action getCommentAction corresponds with /users/{slug}/comments/{id} path.

With @Prefix("some_prefix") generated path will be /users/{slug}/some_prefix/comments/{id}

And by using the @NoRoute method-level annotation, route won't be generated.

这篇关于symfony2 FOSRestBundle 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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