Add a CSV export response in your [Symfony] controller.
- PHP >= 7.4
- Symfony >= 4.4
Use [Composer] to install this package:
composer require ilbee/csv-response
Simply return a CSVResponse object in your Symfony controller, and you will be able to download a CSV file.
Here’s a simple example:
<?php
// ./src/Controller/MyController.php
namespace App\Controller;
use Ilbee\CSVResponse\CSVResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class MyController extends AbstractController
{
/**
* @Route("/download-csv", name="download_csv")
*/
public function downloadCsv(): CSVResponse
{
$data = [];
$data[] = [
'firstName' => 'Marcel',
'lastName' => 'TOTO',
];
$data[] = [
'firstName' => 'Maurice',
'lastName' => 'TATA',
];
return new CSVResponse($data);
}
}
- CSVResponse: This class generates an HTTP response that will trigger a CSV file download based on the provided data.
- Data Example: You can replace the
$data
array with your own data, fetched from a database or other sources.
Special thanks to Paul Mitchum and Dan Feder for their contributions!