symfony2 FOSRestBundle 注释

2024-05-14

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

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

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

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

@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 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 https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/6-automatic-route-generation_multiple-restful-controllers.md :

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

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

通过此配置,路由名称将变为: api_vote_user_comment

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

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] 
}

现在动作 getCommentAction 对应于/用户/{slug}/评论/{id} path.

使用 @Prefix("some_prefix") 生成的路径将是 /users/{slug}/一些前缀/评论/{id}

并通过使用@NoRoute方法级注解,不会生成路由。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

symfony2 FOSRestBundle 注释 的相关文章

随机推荐