Here is the controller method I have implemented :
public function download(
string $id,
ChannelRepository $channelRepository,
CustomerNewsletterRepository $customerNewsletterRepository
): Response
{
$channel = $channelRepository->find($id);
$customerNewsletters = $customerNewsletterRepository->findAllIterable($channel);
set_time_limit(3600);
ob_flush();
$response = new StreamedResponse();
$response->headers->set('X-Accel-Buffering', 'no');
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
'export.csv'
);
$response->headers->set('Content-Disposition', $disposition);
$response->setCallback(function () use ($customerNewsletters): void {
$fp = fopen('php://output', 'w');
fputcsv($fp, ['Email', 'Date'], ';');
/** @var CustomerNewsletter $customerNewsletter */
foreach ($customerNewsletters as $customerNewsletter) {
fputcsv($fp, [
$customerNewsletter->getEmail(),
$customerNewsletter->getCreatedAt()->format('d/m/y H:i'),
], ';');
flush();
}
fclose($fp);
});
return $response->send();
}
It works as expected. When I click on the download button, I can get a file
But, in the dev.log
I can see a critical error :
grep "CRITI" dev.log
[2023-07-07T11:35:23.192510+00:00] request.CRITICAL: Uncaught PHP Exception LogicException: "The content cannot be set on a StreamedResponse instance." at website/vendor/symfony/http-foundation/StreamedResponse.php line 124 {"exception":"[object] (LogicException(code: 0): The content cannot be set on a StreamedResponse instance. at website/vendor/symfony/http-foundation/StreamedResponse.php:124)"} []
How to avoid this behaviour ?
I am working with Sylius, and it was due to a plugin I have installed.
Here is the issue on Github : https://github.com/spinbits/sylius-google-analytics-plugin/issues/33