ilbee / csv-response
Symfony component allow you to respond CSV contents directly in your controller
1.9.0
2026-03-27 15:46 UTC
Requires
- php: >=7.4 <9
- symfony/http-foundation: ^5.4 || ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^9.6
- dev-main
- 1.9.0
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- dev-fix/data-size-limit
- dev-fix/unhandled-object-types
- dev-fix/header-injection-make-disposition
- dev-feature/drop-symfony4-add-symfony8
- dev-chore/add-changelog-fix-ci-badge
- dev-fix/security-header-injection-csv-injection
- dev-feature/streamed-csv-response
- dev-feature/csv-injection-protection
- dev-feature/support-iterable-data
- dev-feature/disable-header-row
- dev-feature/configurable-date-format
- dev-feature/add-utf8-bom-option
- dev-fix/handle-null-bool-array-values
- dev-fix/datetime-immutable-formatting
- dev-feature/modernize-ci
- dev-ilbee-patch-1
This package is auto-updated.
Last update: 2026-03-27 15:46:56 UTC
README
A Symfony component that lets you return CSV file downloads directly from your controllers.
Table of Contents
- Features
- Installation
- Usage
- Which class should I use?
- Contributing
- Credits
- License
- Full Documentation
Features
- Two response classes for different use cases:
| Class | Extends | Best for |
|---|---|---|
CSVResponse |
Response |
Small to medium datasets (buffered in memory) |
StreamedCSVResponse |
StreamedResponse |
Large datasets (streamed row by row, constant memory) |
- Automatic header row generation from array keys (can be disabled)
- Configurable separator (semicolon by default, comma, etc.)
- Custom file name support
- DateTime objects are automatically formatted (configurable format)
- Optional UTF-8 BOM for Excel compatibility
- CSV injection protection (formula sanitization)
- Optional row limit (
maxRows) to prevent unbounded memory usage - Strict type handling: clear errors for unsupported object types
- Accepts arrays, iterables, generators, or callables as data source
- No configuration required — just install and use
Installation
composer require ilbee/csv-response
Usage
Basic example
use Ilbee\CSVResponse\CSVResponse; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Attribute\Route; class ExportController extends AbstractController { #[Route('/export', name: 'export_csv')] public function export(): CSVResponse { $data = [ ['firstName' => 'Marcel', 'lastName' => 'TOTO'], ['firstName' => 'Maurice', 'lastName' => 'TATA'], ]; return new CSVResponse($data); } }
This triggers a download of CSVExport.csv with the content:
firstName;lastName
Marcel;TOTO
Maurice;TATA
Streaming large exports
use Ilbee\CSVResponse\StreamedCSVResponse; class ExportController extends AbstractController { #[Route('/export/large', name: 'export_large_csv')] public function exportLarge(UserRepository $repository): StreamedCSVResponse { // Callable is invoked at send-time — no data buffered in memory return new StreamedCSVResponse(function () use ($repository) { foreach ($repository->findAllIterator() as $user) { yield [ 'id' => $user->getId(), 'email' => $user->getEmail(), 'name' => $user->getName(), ]; } }); } }
Custom file name and separator
use Ilbee\CSVResponse\CSVResponseInterface; return new CSVResponse($data, 'users.csv', CSVResponseInterface::COMMA);
UTF-8 BOM (for Excel)
return new CSVResponse($data, 'users.csv', CSVResponseInterface::SEMICOLON, true);
Custom date format
return new CSVResponse($data, 'users.csv', CSVResponseInterface::SEMICOLON, false, 'd/m/Y');
Limit number of rows
// Throws OverflowException if data exceeds 10 000 rows return new CSVResponse($data, 'users.csv', CSVResponseInterface::SEMICOLON, false, 'Y-m-d H:i:s', true, true, 10000);
Without header row
return new CSVResponse( $data, 'users.csv', CSVResponseInterface::SEMICOLON, false, 'Y-m-d H:i:s', false );
Which class should I use?
| Scenario | Class |
|---|---|
| Small datasets (< 1000 rows) | CSVResponse |
Need to access content after creation (getContent()) |
CSVResponse |
| Large datasets or unknown size | StreamedCSVResponse |
| Database cursor / generator source | StreamedCSVResponse |
| Memory-constrained environment | StreamedCSVResponse |
Both classes share the same constructor signature and support the same features. The only difference is how data is written to the response.
Contributing
composer install composer test # PHPUnit composer phpstan # Static analysis composer cs-check # Code style check composer cs-fix # Auto-fix code style
Credits
Special thanks to Paul Mitchum and Dan Feder for their contributions.