Installation
composer require ee/dataexporter-bundle
Add to config/bundles.php:
return [
// ...
EE\DataExporterBundle\EEDataExporterBundle::class => ['all' => true],
];
First Export (CSV)
use EE\DataExporterBundle\Exporter\CsvExporter;
use EE\DataExporterBundle\Writer\StreamWriter;
$exporter = new CsvExporter();
$writer = new StreamWriter('php://output', 'w');
$exporter->export($writer, ['headers' => ['ID', 'Name'], 'data' => [[1, 'John'], [2, 'Jane']]]);
Key Files to Review
Resources/doc/index.md (official docs)src/Exporter/ (core exporters: CsvExporter, XmlExporter, etc.)src/Writer/ (writers for files, streams, or in-memory)Data Preparation
Normalize data into an associative array with headers and data keys:
$data = [
'headers' => ['user_id', 'email', 'created_at'],
'data' => [
[1, 'john@example.com', '2023-01-01'],
[2, 'jane@example.com', '2023-01-02'],
],
];
Exporter Selection Choose an exporter based on output format:
$exporter = match ($format) {
'csv' => new CsvExporter(),
'xml' => new XmlExporter(),
'json' => new JsonExporter(),
'html' => new HtmlExporter(),
'xls' => new XlsExporter(), // Requires PhpSpreadsheet
default => throw new \InvalidArgumentException('Unsupported format'),
};
Writer Integration
$writer = new StreamWriter('path/to/file.csv', 'w');
$writer = new MemoryWriter();
$exporter->export($writer, $data);
$output = $writer->getContent(); // Return as response
$writer = new StreamWriter('php://output', 'w');
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
Laravel-Specific Integration Use a Service Provider to bind exporters:
$this->app->bind('exporter.csv', function () {
return new CsvExporter();
});
Then inject via constructor:
public function __construct(private CsvExporter $csvExporter) {}
Batch Processing
For large datasets, use chunking with QueryBuilder:
$exporter = new CsvExporter();
$writer = new StreamWriter('php://output', 'w');
$exporter->setBatchSize(1000); // Process 1000 rows at a time
$exporter->export($writer, $data);
Memory Limits
memory_limit.MemoryWriter with chunking or stream directly to a file:
$writer = new StreamWriter('php://temp', 'w+');
XLS Exporter Dependencies
XlsExporter requires phpoffice/phpspreadsheet (not auto-installed).composer require phpoffice/phpspreadsheet
Header Injection
$headers = array_map('htmlspecialchars', $headers);
Encoding Issues
é, ü) may corrupt output.$writer = new StreamWriter('file.csv', 'w');
$writer->setCharset('UTF-8');
XML/HTML Escaping
XmlExporter::setEscapeHtml(true) or pre-escape data:
$data['data'] = array_map(function ($row) {
return array_map('htmlspecialchars', $row);
}, $data['data']);
Dynamic Headers
Use get_headers() from Eloquent collections:
$headers = array_keys($users->first()->toArray());
$data = ['headers' => $headers, 'data' => $users->toArray()];
Custom Writers
Extend AbstractWriter for custom storage (e.g., S3, database):
class S3Writer extends AbstractWriter {
public function write($data) {
Storage::disk('s3')->put('export.csv', $data);
}
}
Laravel Response Helper Create a helper to stream exports:
function exportToResponse($data, $format) {
$exporter = match ($format) {
'csv' => new CsvExporter(),
// ...
};
$writer = new StreamWriter('php://output', 'w');
$exporter->export($writer, $data);
return response()->stream();
}
Debugging
StreamWriter to a temp file for debugging:
$writer = new StreamWriter(storage_path('app/debug_export.csv'), 'w');
DataExporterBundle\Validator\DataValidator to check structure:
$validator = new DataValidator();
if (!$validator->validate($data)) {
throw new \RuntimeException('Invalid export data');
}
Performance
$exporter = new CsvExporter();
$exporter->setIndexedBy(false); // Faster for unordered data
How can I help you explore Laravel packages today?