2017-06-07 73 views
0

我有一點問題,因爲我必須在Symfony3中顯示帶有數據庫的json日期。如何在symfony中顯示帶有數據庫的json數據

我控制器

/** 
    * @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 
    ); 
} 

這是我在html.twig

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

佈局,但我不知道爲什麼什麼都不顯示。 請幫助我:)

+0

你爲什麼要把它編碼爲json? – goto

+0

那麼你想要返回json,還是渲染一個樹枝模板? –

+0

這是我對工作的要求。 –

回答

1

好吧用我的推理能力(因爲你的問題是有點模糊),我會做出相反的假設爲其他人做了,並說使用JSON helper功能(自3.2)。

return $this->json($myThings); 

如果您使用的是舊版本,則需要返回正確的Response object以及標頭等。

0

試試這個:

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

如果它不工作,嘗試改變,你想打印一個數組

$data = json_encode($row, true); 

$data = json_decode(json_encode($row), true);

如果你想打印字符串,請在你的樹枝內試試這個

{{ dump(comments) }} 

,而不是這個

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

不,他有@template註釋 – goto

+0

ops抱歉更新與新的解決方案的答案,謝謝@goto –

+0

是的,probalby我必須使用對象響應,但我不知道如何 –

0

json_encode返回一個字符串。

嫩枝

{{ dump(comments) }} 
相關問題