2014-02-11 37 views
0

我正在嘗試創建一個csv並將其下載到瀏覽器。從我能找到的東西來看,在Symfony2中幾乎有這個ZERO文檔。Symfony2創建並下載CSV

這裏是我的代碼:

// Pass in all of the variables that will make up the pdf... 
    $response = $this->renderView('WICPurchaseOrderBundle:PurchaseOrder:csv.html.twig', array("data"=>$data 
     ) 
    ); 
    $response->headers->set('Content-Type', 'text/csv'); // This is line 929 in the error. 
    $response->headers->set('Content-Disposition', 'attachment; filename="teams.csv"'); 

    return $response; 

這是錯誤我得到:

Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/symfonydev/src/WIC/PurchaseOrderBundle/Controller/PurchaseOrderController.php line 929 

任何有CSV在Symfony2中的經驗,並能幫助我嗎?謝謝!

回答

2

我在Symfony2應用程序中有一個成功的CSV下載,看起來您已經接近正確了。改變這一行:

$response = $this->renderView('WICPurchaseOrderBundle:PurchaseOrder:csv.html.twig', 
    array("data"=>$data) 
); 

這樣:

$response = $this->render('WICPurchaseOrderBundle:PurchaseOrder:csv.html.twig', 
    array("data"=>$data) 
); 

renderView()方法渲染模板,並返回其內容,但不創建一個響應。該render()方法返回一個包含從模板內容的Response對象:

請參閱該文檔有關Rendering a template

+0

哇,我不能相信這是這麼簡單。非常感謝你的幫助! – LargeTuna