ilbee/csv-response

Symfony component allow you to respond CSV contents directly in your controller

1.3.0 2025-03-10 09:40 UTC

This package is auto-updated.

Last update: 2025-03-10 09:51:31 UTC


README

Active repository License PHP Composer

Add a CSV export response in your [Symfony] controller.

๐Ÿ“– Table of Contents

  1. โ„น๏ธ Prerequisites
  2. โš™ Installation
  3. ๐Ÿ› ๏ธ How to Use
  4. ๐Ÿ”— Useful Links
  5. ๐Ÿ™ Thanks

โ„น๏ธ Prerequisites

  • PHP >= 7.4
  • Symfony >= 4.4

โš™ Installation

Use [Composer] to install this package:

composer require ilbee/csv-response

๐Ÿ› ๏ธ How to use ?

Simply return a CSVResponse object in your Symfony controller, and you will be able to download a CSV file.

Hereโ€™s a simple example:

<?php
// ./src/Controller/MyController.php
namespace App\Controller;

use Ilbee\CSVResponse\CSVResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class MyController extends AbstractController
{
    /**
     * @Route("/download-csv", name="download_csv") 
     */
    public function downloadCsv(): CSVResponse
    {
        $data = [];
        $data[] = [
            'firstName' => 'Marcel',
            'lastName'  => 'TOTO',
        ];   
        $data[] = [
            'firstName' => 'Maurice',
            'lastName'  => 'TATA',
        ];
        
        return new CSVResponse($data);
    }
}

Explanation

  1. CSVResponse: This class generates an HTTP response that will trigger a CSV file download based on the provided data.
  2. Data Example: You can replace the $data array with your own data, fetched from a database or other sources.

๐Ÿ”— Useful Links

  • Symfony - Official Symfony Documentation
  • Composer - PHP Dependency Manager

๐Ÿ™ Thanks

Special thanks to Paul Mitchum and Dan Feder for their contributions!