我的以下代码未返回任何错误,但是,它返回的是HTML页面中的数据字符串,而不是返回CSV文件。我不知道我在symfony设置中错过了什么。
$em = $this->getDoctrine()->getManager();
$sql = "select name, id from table";
$stmt = $em->getConnection()->getWrappedConnection()->prepare($sql);
$stmt->execute();
$source = new PDOStatementSourceIterator($stmt);
$format='csv';
$export_to='php://output';
$exporterWriter='\Exporter\Writer\\'.ucfirst($format).'Writer';
$writer = new $exporterWriter($export_to);
Handler::create($source, $writer)->export();
$response = new Response();
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="data.csv"');
return $response;