2014-02-28 43 views
1

我學習的Symfony 2,我有問題:Symfony 2基本控制器 - 返回響應?

我的代碼是:

public function showListAction() 
{ 
    $ar = $this->getDoctrine() 
       ->getRepository('AcmeHelloBundle:ModGlobalAspectRatio') 
       ->findAll(); 

    return $this->render('AcmeHelloBundle:Test:showList.html.php', array('recs' => $ar)); 
} 

public function updateAction($id, $name) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $ar = $em->getRepository('AcmeHelloBundle:ModGlobalAspectRatio')->find($id); 

    $ar->setName($name); 
    $em->flush(); 

    $content = $this->forward('AcmeHelloBundle:Hello:showList'); 

    return new Response($content); 
} 

我在 「更新」 我的第一個動作 「showList」 運行。

沒有return new Response()我有錯誤:

爲什麼要使用return new Response後我有:

The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller? 

爲什麼..其litttle愚蠢..

我的主要問題是,我不明白這一點文本在頁頂:

HTTP/1.0 200 OK Cache-Control: no-cache Date: Fri, 28 Feb 2014 19:42:48 GMT X-Debug-Token: f0ada0 X-Debug-Token-Link: /symfony2/web/app_dev.php/_profiler/f0ada0 

我不要編輯這個奇怪的字符串..我如何刪除它?

謝謝! :)

+0

爲什麼不使用'return $ this-> forward('AcmeHelloBundle:Hello:showList');'而不是使用完全無用的'$ content'變量? –

回答

1

簡單地返回$content

public function updateAction($id, $name) 
{ 
    //... 

    $content = $this->forward('AcmeHelloBundle:Hello:showList'); 

    return $content; 
} 

編輯:

你的控制器動作應該返回一個Response對象。當您在基本控制器中調用$this->forward();時,它將返回所需的Response,而這又必須由您的控制器操作返回。