Install via Composer (if not archived, but since it is, check for forks or alternatives like league/csv + custom logic):
composer require bluetea/export-bundle
Note: Due to archival, verify compatibility with your Symfony version (likely Symfony2).
Enable the Bundle in app/AppKernel.php:
new BlueTea\ExportBundle\BlueTeaExportBundle(),
Basic CSV Export Controller:
use BlueTea\ExportBundle\Exporter\ExporterInterface;
use BlueTea\ExportBundle\Exporter\CsvExporter;
class ExportController extends Controller
{
public function exportAction()
{
$exporter = $this->get('blue_tea_export.exporter.csv');
$data = $this->getDoctrine()->getRepository('AppBundle:User')->findAll();
return $exporter->export($data, 'users.csv');
}
}
Route the Controller:
# app/config/routing.yml
export_users:
path: /export/users
defaults: { _controller: AppBundle:Export:export }
Export a Doctrine entity (e.g., User) to CSV with headers:
$exporter = $this->get('blue_tea_export.exporter.csv');
$users = $this->getDoctrine()->getRepository('AppBundle:User')->findAll();
$headers = ['ID', 'Email', 'Name']; // Map to entity properties
return $exporter->export($users, 'users.csv', $headers);
Dependency Injection:
Inject ExporterInterface (or CsvExporter directly) into services/controllers:
public function __construct(ExporterInterface $exporter) {
$this->exporter = $exporter;
}
Data Transformation: Use factories to format data before export. Example for custom objects:
$factory = new \BlueTea\ExportBundle\Factory\ArrayFactory();
$formattedData = $factory->transform($users); // Converts entities to arrays
$this->exporter->export($formattedData, 'output.csv');
Streaming Large Datasets: For memory efficiency, stream rows instead of loading all data:
$query = $this->getDoctrine()->getRepository('AppBundle:User')->createQueryBuilder('u');
$exporter->setStreamingMode(true);
$exporter->export($query->getResult(), 'large_users.csv');
Dynamic Headers: Auto-generate headers from Doctrine metadata:
$metadata = $this->getDoctrine()->getManager()->getMetadataFactory()->getMetadataFor('AppBundle:User');
$headers = array_map(function($field) { return $field->getName(); }, $metadata->getFieldNames());
$form = $this->createFormBuilder()
->add('export', SubmitType::class, ['label' => 'Export CSV'])
->getForm();
if ($form->isSubmitted() && $form->isValid()) {
return $this->exportAction();
}
$eventManager->addEventListener(
KernelEvents::TERMINATE,
function () use ($exporter) {
$exporter->export($data, 'audit.csv');
}
);
AbstractExporter to support new formats (e.g., JSON):
class JsonExporter extends AbstractExporter
{
protected function writeHeader(array $headers) { /* ... */ }
protected function writeRow(array $row) { /* ... */ }
}
Archived Status:
league/csv or matthiasnoback/symfony-export-bundle for modern Symfony.Doctrine Dependency:
Memory Limits:
setStreamingMode(true) or chunk queries:
$exporter->export(array_chunk($users, 1000), 'chunked_users.csv');
Header Mismatch:
array_keys($data[0]) for auto-detection:
$headers = array_keys(reset($users));
Encoding Issues:
$exporter->setEncoding('UTF-8');
count($data) > 0).writeRow() (check logs).var/exports/).Custom Factories:
Override ArrayFactory to transform complex objects:
class UserFactory extends ArrayFactory
{
public function transform($user)
{
return [
'id' => $user->getId(),
'full_name' => $user->getFirstName() . ' ' . $user->getLastName(),
];
}
}
Post-Export Actions:
Hook into postExport event (if supported) or chain services:
$exporter->export($data, 'file.csv');
$this->mailer->send('export@ready@example.com', 'Export ready', ['file' => 'file.csv']);
Configuration:
Override defaults in config.yml:
blue_tea_export:
csv:
delimiter: ';'
enclosure: '"'
escape_char: '\\'
line_ending: "\r\n"
include_headers: true
cache/ and clear on data changes.$this->denyAccessUnlessGranted('ROLE_EXPORT', $user);
ExporterInterface in PHPUnit:
$mockExporter = $this->createMock(ExporterInterface::class);
$mockExporter->expects($this->once())->method('export');
$this->controller->setExporter($mockExporter);
How can I help you explore Laravel packages today?