Installation:
composer require ajgl/csv-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3.x):
// config/bundles.php
return [
// ...
Ajgl\CsvBundle\AjglCsvBundle::class => ['all' => true],
];
First Use Case:
Import the Ajgl\Csv\Csv class and use it to parse a CSV file:
use Ajgl\Csv\Csv;
$csv = new Csv();
$data = $csv->parse(file_get_contents('path/to/file.csv'));
Or generate a CSV from an array:
$csv = new Csv();
$csvContent = $csv->generate($dataArray);
Parsing CSV Files:
$csv = new Csv();
$data = $csv->parse(file_get_contents('input.csv'), ',', '"');
utf-8).parseStream() for memory efficiency:
$stream = fopen('large_file.csv', 'r');
$data = $csv->parseStream($stream);
Generating CSV:
$csv = new Csv();
$csvContent = $csv->generate([
['Name', 'Email'],
['John', 'john@example.com'],
], ',', '"');
$data is associative.utf-8) for non-ASCII characters.Integration with Symfony Services:
Ajgl\Csv\Csv service in config/services.yaml:
services:
Ajgl\Csv\Csv: ~
public function __construct(private Csv $csv) {}
Command-Line Usage:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ImportCsvCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$csv = new Csv();
$data = $csv->parse(file_get_contents($this->getInputFile()));
// Process $data...
}
}
Form Handling:
public function uploadAction(Request $request)
{
$file = $request->files->get('csv_file');
$csv = new Csv();
$data = $csv->parse($file->getContent());
// Validate and save $data...
}
Deprecated Bundle:
league/csv for modern projects.Memory Issues:
parse() loads the entire file into memory. For large files, always use parseStream():
$stream = fopen('large.csv', 'r');
$data = $csv->parseStream($stream);
Encoding Problems:
é, ü). Explicitly set encoding:
$csv->parse($content, ',', '"', 'utf-8');
Configuration Overrides:
No Built-in Validation:
symfony/validator.Check Delimiters/Enclosures:
; vs ,) or enclosures (e.g., " vs '). Inspect the CSV file manually or use a tool like CSVLint.Streaming Errors:
$stream = fopen('file.csv', 'r');
try {
$data = $csv->parseStream($stream);
} finally {
fclose($stream);
}
Symfony Dependency Conflicts:
symfony/framework-bundle:^4.1 is installed (not older versions).Custom Parsing Logic:
Ajgl\Csv\Csv class to add custom parsing rules:
class CustomCsv extends Csv
{
public function parseWithCustomRules($content)
{
$data = $this->parse($content);
// Add custom logic (e.g., data transformation)
return $data;
}
}
Event Listeners:
kernel.request) to log or modify CSV operations:
// src/EventListener/CsvListener.php
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMasterRequest() && $event->getRequest()->query->has('parse_csv')) {
$csv = new Csv();
$data = $csv->parse(file_get_contents('temp.csv'));
// Log or process $data...
}
}
Integration with Doctrine:
$entityManager = $this->getDoctrine()->getManager();
foreach ($csvData as $row) {
$entity = new YourEntity();
$entity->setField($row['column']);
$entityManager->persist($entity);
}
$entityManager->flush();
Batch Processing:
$chunkSize = 1000;
$stream = fopen('large.csv', 'r');
$i = 0;
while (($data = $csv->parseStream($stream, $chunkSize)) !== false) {
// Process $data chunk
$i++;
}
Caching:
private $cachedData = [];
public function getCachedData($filePath)
{
if (!isset($this->cachedData[$filePath])) {
$csv = new Csv();
$this->cachedData[$filePath] = $csv->parse(file_get_contents($filePath));
}
return $this->cachedData[$filePath];
}
How can I help you explore Laravel packages today?