2014-02-11 82 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