2014-10-22 44 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"} 
    ] 
    } 
}