2011-04-27 65 views
13

我试图设置一个使用symfony2的json示例。
我创建了一个测试包,测试实体(“信息”),成立了ORM等
消息(表)具有以下栏目:ID,标题,文字
我试图揭露路线*/MYDOMAIN /消息会暴露一个JSON接口信息表(小名单)Symfony2 JSON示例

我尝试的第一个方法是:

创建一个使用Symfony\Component\HttpFoundation\Response并具有功能的MessageController类像这样:

public function testAction() { 
    $response = new Response(json_encode(**code_req_here**)); 
    return $response; 
} 

,并设置像这样的路线:

test: 
    pattern: /test 
    defaults: { _controller: myProjectmyTestBundle:Message:test, _format: json} 
    requirements: { _format: (xml|json), _method: GET } 
  1. 这是一个正确的方法?
  2. 我对code_req_here有什么要求?

我尝试了第二种方法是通过使用FOS/RestBundle但没能正确完成引导,因为我明白,所以

  1. 请提供关于如何做一个小指南(只)这与FOS/RestBundle
+0

请使用[代码格式化](http://stackoverflow.com/editing-help)为您的下一个问题。 – Czechnology 2011-04-27 23:02:18

回答

13

这是一个正确的方法?

是的,我喜欢它,但我会修改路由规则有点像这样:

test: 
    pattern: /test.{_format} 
    defaults: { _controller: myProjectmyTestBundle:Message:test, _format: json} 
    requirements: { _format: (xml|json), _method: GET } 

什么我穿上code_req_here?

把你想要转换成json格式的数组。 ex。阵列(阵列( 'ID'=> 1, '值'=> '测试'),阵列( 'ID'=> 2, '值'=> '智能'))