Installation
composer require bugloos/export-bundle
Ensure your project meets the requirements: PHP 7.4+ and Symfony 4.4+.
Enable the Bundle
Add to config/bundles.php:
Bugloos\ExportBundle\BugloosExportBundle::class => ['all' => true],
First Export Use Case Create a controller method to export a simple array as CSV:
use Bugloos\ExportBundle\Exporter\ExporterInterface;
use Symfony\Component\HttpFoundation\Response;
public function export(ExporterInterface $exporter): Response
{
$data = [
['Name', 'Email'],
['John Doe', 'john@example.com'],
];
return $exporter->export($data, 'export.csv');
}
Key Classes to Explore
ExporterInterface: Core interface for exports.Exporter: Default implementation (check src/Exporter/Exporter.php).Format\Csv: CSV-specific logic (if available in future updates).Exporting Collections Convert Doctrine collections or Eloquent results to arrays:
$users = $entityManager->getRepository(User::class)->findAll();
$exportData = array_map(fn(User $user) => [$user->getName(), $user->getEmail()], $users);
return $exporter->export($exportData, 'users.csv');
Dynamic Headers Use a callback to generate headers dynamically:
$headers = array_map(fn($key) => ucfirst($key), array_keys($data[0]));
$exportData = array_merge([$headers], $data);
Streaming Large Datasets For memory efficiency, stream row-by-row (if supported in future versions):
$exporter->streamExport($data, 'large_export.csv');
Integration with Symfony Forms Use form data directly:
$formData = $form->getData();
$exportData = array_map(fn($item) => [$item['name'], $item['value']], $formData);
Format\AbstractFormat to add new formats (e.g., JSON, XML).kernel.response to auto-export based on routes.No Active Maintenance
Limited Documentation
HttpFoundation for fallback responses.CSV Quirks
$escapedData = array_map(
fn($row) => array_map('str_getcsv', $row),
$data
);
Dependency Conflicts
symfony/csv or phpoffice/phpexcel if installed.Content-Disposition is set correctly:
$response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
\Log::debug('Export data:', ['data' => $data]);
use Symfony\Component\HttpFoundation\StreamedResponse;
$response = new StreamedResponse();
$response->setCallback(fn() => $this->streamCsv($data));
Add New Formats
Extend AbstractFormat and register as a service:
# config/services.yaml
Bugloos\ExportBundle\Format\JsonFormat: ~
Custom Exporter
Implement ExporterInterface for specialized logic:
class CustomExporter implements ExporterInterface {
public function export(array $data, string $filename): Response {
// Custom logic
}
}
Configuration
Override defaults in config/packages/bugloos_export.yaml (if supported):
bugloos_export:
default_format: 'csv'
delimiter: ';'
How can I help you explore Laravel packages today?