dongyulan6251 2017-06-07 15:04
浏览 95
已采纳

如何在symfony中显示数据库的json数据

I have a little problem, because I have to show json date with database in Symfony3.

My controller

/**
     * @Route("/api/rest", name="rest_api")
     * 
     * 
     * @Template
     */
public function indexAction(Request $Request) {

    $Repo = $this->getDoctrine()->getRepository('CommonUserBundle:Comment');
    $row = $Repo->findAll();


    $data = json_encode($row, true);

    return array(
        'comment' => $data
    );
}

And this is my layout in html.twig

{% for comments in comment %}
           {{ comments }}
{% endfor %}

But I don't know why nothing show. Help me please :)

  • 写回答

3条回答 默认 最新

  • dongxia026531 2017-06-07 15:19
    关注

    Ok using my deductive skills (as your question is a bit vague) I am going to make the opposite assumption as everyone else did and say use the JSON helper function (Since 3.2).

    return $this->json($myThings);
    

    If you are working in older versions you need to return a proper Response object with the headers and such.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?