audunru/export-response
Composer 安装命令:
composer require audunru/export-response
包简介
Export Laravel JSON to responses to other formats, e.g. CSV
README 文档
README
Currently supported:
- CSV
- XLSX
- XML
Installation
Step 1: Install with Composer
composer require audunru/export-response
Depending on which formats you want to export to, you will have to install additional packages:
| Format | Package |
|---|---|
| CSV | spatie/simple-excel |
| XLSX | spatie/simple-excel |
| XML | spatie/array-to-xml |
Step 2: Add middleware to your routes
To allow exports for all your API endpoints, add middleware to Kernel.php:
'api' => [ 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, \audunru\ExportResponse\Middleware\ExportCsv::class, \audunru\ExportResponse\Middleware\ExportXlsx::class, \audunru\ExportResponse\Middleware\ExportXml::class, ],
To add it to one particular API resource, you can use this in api.php:
Route::apiResource('documents', DocumentController::class) ->middleware([ ExportCsv::class, ExportXlsx::class, ExportXml::class ]) ->name('documents');
You can specify an array key which will be used to retrieve the data. "Dot" notation is supported.
Route::apiResource('documents', DocumentController::class) ->middleware([ ExportCsv::with([ 'key' => 'data', ]), ExportXlsx::with([ 'key' => 'data', ]), ExportXml::class::with([ 'key' => 'data', ]), ]) ->name('documents');
You can also add the middleware to the $middlewareGroups and $routeMiddleware arrays in app/Http/Kernel.php:
protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:api', \Illuminate\Routing\Middleware\SubstituteBindings::class, // Add ExportCsv middleware to all requests. "data" is the name of // the key to retrieve using "dot" notation. 'csv:data', ], ]; protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'csv' => \audunru\ExportResponse\Middleware\ExportCsv::class, ];
Exporting from controller
Instead of using middleware, you can perform the export in the controller:
class ProductController extends Controller { public function csv() { $products = Product::all(); return $products->toCsv('filename.csv'); }
Lazy collections are also supported:
class ProductController extends Controller { public function csv() { $products = Product::lazy(); return $products->toCsv('filename.csv'); }
Please use lazy collections when you can. During testing, using Product::lazy() to export 10,000 products took about 2MB of memory, compared to 44 MB of memory using Product::all(). Both exports took the same amount of time (around 45 seconds).
Step 3: Exporting a response
In order to retrieve an API response as CSV instead of JSON, send a request to your API with the Accept header set to text/csv.
For XML, set the header to application/xml.
For XLSX, set the header to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.
Configuration
Publish the configuration file by running:
php artisan vendor:publish --tag=export-response-config
Development
Testing
Run tests:
composer test
audunru/export-response 适用场景与选型建议
audunru/export-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.97k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「csv」 「export」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 audunru/export-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 audunru/export-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 audunru/export-response 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
Bulk export of sylius resources
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Yii2 export extension
laravel facade to read/write csv file
统计信息
- 总下载量: 4.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-17