PHP code example of ilbee / csv-response

1. Go to this page and download the library: Download ilbee/csv-response library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ilbee / csv-response example snippets


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);
    }
}

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(),
                ];
            }
        });
    }
}

use Ilbee\CSVResponse\CSVResponseInterface;

return new CSVResponse($data, 'users.csv', CSVResponseInterface::COMMA);

return new CSVResponse($data, 'users.csv', CSVResponseInterface::SEMICOLON, true);

return new CSVResponse($data, 'users.csv', CSVResponseInterface::SEMICOLON, false, 'd/m/Y');

// 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);

return new CSVResponse(
    $data,
    'users.csv',
    CSVResponseInterface::SEMICOLON,
    false,
    'Y-m-d H:i:s',
    false
);