2014-10-22 45 views
0

我在我的SF2项目中使用了Bazinga Hateoas和Fosrest。Bizinga Hateoas与Symfony的多个链接

在API调用的一个,我想与当前用户或用户ID像这样提供给显示器的朋友链接:

{ 
     "_links": { 
      "self": { "href": "https://stackoverflow.com/users/1" }, 
      "friends": [ 
       { "href": "https://stackoverflow.com/users/2" }, 
       { "href": "https://stackoverflow.com/users/3" }, 
      ] 
     }, 
    } 

我在Entity.User.yml文件中使用下面的代码:

relations: 
    - 
     rel: self 
     href: 
     route: api_1_get_users 
     parameters: 
      id: expr(object.getId()) 
     absolute: true 
    - 
     rel: expr(object.findFriends(object.getId())) 
     href: 
     route: api_1_get_users 
     parameters: 
      id: expr(object.getId()) 
     absolute: true 

我已经把 “findFriends” 方法回购itory,但它不能在yml文件中访问。我想这不是正确的做事方式。

我已经通过https://github.com/willdurand/Hateoas,但无法弄清楚如何去做。请指导我如何实现这一目标...

任何帮助将不胜感激!

请指导我如何才能做到这一点

回答

2

这是你如何与@RelationProvider工作。

/** 
* Note: 
* ==== 
* RelationProvider takes the method name which returns the relations. 
* 
* @Hateoas\RelationProvider("addRelations") 
*/ 
class LinkContainingResource 
{ 
    public function addRelations($object, ClassMetadataInterface $classMetadata) 
    { 
     /** 
     * Important Note: 
     * =============== 
     * Relation is actually an Hateoas\Configuration\Relation object, 
     * NOT \Hateoas\Configuration\Annotation\Relation 
     */ 
     return [new Relation('relation_name', 'link1'), 
       new Relation('relation_name', 'link2'), 
       new Relation('relation_name', 'link3')]; 
    } 
} 

的Json /哈尔结果:

{ 
    "_links": { 
    "relation_name": [ 
     {"href": "link1"}, 
     {"href": "link2"}, 
     {"href": "link3"} 
    ] 
    } 
}