ilbee/csv-response
Composer 安装命令:
composer require ilbee/csv-response
包简介
Symfony component allow you to respond CSV contents directly in your controller
README 文档
README
A Symfony component that lets you return CSV file downloads directly from your controllers.
Table of Contents
- Features
- Installation
- Usage
- Which class should I use?
- Contributing
- Credits
- License
- Full Documentation
Features
- Two response classes for different use cases:
| Class | Extends | Best for |
|---|---|---|
CSVResponse |
Response |
Small to medium datasets (buffered in memory) |
StreamedCSVResponse |
StreamedResponse |
Large datasets (streamed row by row, constant memory) |
- Automatic header row generation from array keys (can be disabled)
- Configurable separator (semicolon by default, comma, etc.)
- Custom file name support
- DateTime objects are automatically formatted (configurable format)
- Optional UTF-8 BOM for Excel compatibility
- CSV injection protection (formula sanitization)
- Optional row limit (
maxRows) to prevent unbounded memory usage - Strict type handling: clear errors for unsupported object types
- Accepts arrays, iterables, generators, or callables as data source
- No configuration required — just install and use
Installation
composer require ilbee/csv-response
Usage
Basic example
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); } }
This triggers a download of CSVExport.csv with the content:
firstName;lastName
Marcel;TOTO
Maurice;TATA
Streaming large exports
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(), ]; } }); } }
Custom file name and separator
use Ilbee\CSVResponse\CSVResponseInterface; return new CSVResponse($data, 'users.csv', CSVResponseInterface::COMMA);
UTF-8 BOM (for Excel)
return new CSVResponse($data, 'users.csv', CSVResponseInterface::SEMICOLON, true);
Custom date format
return new CSVResponse($data, 'users.csv', CSVResponseInterface::SEMICOLON, false, 'd/m/Y');
Limit number of rows
// 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);
Without header row
return new CSVResponse( $data, 'users.csv', CSVResponseInterface::SEMICOLON, false, 'Y-m-d H:i:s', false );
Which class should I use?
| Scenario | Class |
|---|---|
| Small datasets (< 1000 rows) | CSVResponse |
Need to access content after creation (getContent()) |
CSVResponse |
| Large datasets or unknown size | StreamedCSVResponse |
| Database cursor / generator source | StreamedCSVResponse |
| Memory-constrained environment | StreamedCSVResponse |
Both classes share the same constructor signature and support the same features. The only difference is how data is written to the response.
Contributing
composer install composer test # PHPUnit composer phpstan # Static analysis composer cs-check # Code style check composer cs-fix # Auto-fix code style
Credits
Special thanks to Paul Mitchum and Dan Feder for their contributions.
License
ilbee/csv-response 适用场景与选型建议
ilbee/csv-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 179.99k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「export」 「Symfony controller response」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ilbee/csv-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ilbee/csv-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ilbee/csv-response 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bulk export of sylius resources
The bundle for easy using json-rpc api on your project
Yii2 export extension
Bundle Symfony DaplosBundle
laravel facade to read/write csv file
A simple API extension for SplFileObject
统计信息
- 总下载量: 179.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-26