neoxia/laravel-csv-response 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

neoxia/laravel-csv-response

Composer 安装命令:

composer require neoxia/laravel-csv-response

包简介

Add a CSV response type to Laravel

README 文档

README

Latest Stable Version Build Status Coverage Status

Laravel CSV Response

This package adds a CSV response type to the Laravel ResponseFactory class. Because CSV is a data format, just like JSON, it should be possible to respond to a request with this format.

$data = [
    ['first_name', 'last_name'],
    ['John', 'Doe'],
    ['Jane', 'Doe'],
];

return response()->csv($data);

This small package offers a straightforward solution that deals with conversion, from array or collection of objects to comma separated values string, and character encoding.

Disclaimer

This package is just a pretty cool helper to create CSV responses without pain.

If you want to generate CSV (or Excel) files with a lot of options and more robustness you should take a look at Maatwebsite/Laravel-Excel.

Installation

Require this package with composer using the following command:

composer require neoxia/laravel-csv-response

As of Laravel 5.5, this package will be automatically discovered and registered. For older version of Laravel, add the service provider in config/app.php.

Neoxia\Routing\ResponseFactoryServiceProvider::class,

Usage

Base data format

The csv() method is very flexible about data format. All this examples return exactly the same response.

response()->csv(collect(
    new User(['first_name' => 'John', 'last_name' => 'Doe']),
    new User(['first_name' => 'Jane', 'last_name' => 'Doe']),
));

response()->csv([
    ['first_name', 'last_name'],
    ['John', 'Doe'],
    ['Jane', 'Doe'],
]);

response()->csv([
    ['first_name' => 'John', 'last_name' => 'Doe'],
    ['first_name' => 'Jane', 'last_name' => 'Doe'],
]);

response()->csv("first_name;last_name\r\nJohn;Doe\r\nJane;Doe");

Objects as rows

If the "rows" of the array of data passed to the method are objects, you have to implement the csvSerialize() method in this objects. This method is based in the same principle than the jsonSerialize() method that is already implemented into an Eloquent model. It should return data as an associative array.

For example:

class User
{
    public function csvSerialize()
    {
        return [
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
        ];
    }
}

CSV first row

When the "rows" of the data collection are associative arrays or objects, the package use the keys of the first row to define the first row of the CSV response. This first row is generaly used as column titles in this type of file.

In order to have a consistent response, you have to be sure that every row in the data collection has the same number of values and the keys in the same order.

Other parameters

The csv() function declaration, based on Laravel json() function, is the following.

public function csv($data, $status = 200, array $headers = [], array $options = [])

Status

Typically, you should return your CSV with status 200 Ok but you are allowed to be imaginative. Maybe are you building a full REST-CSV API ;)

Headers

The default headers for this response are the following but you can overwrite it.

[
    'Content-Type' => 'text/csv; charset=WINDOWS-1252',
    'Content-Encoding' => 'WINDOWS-1252',
    'Content-Transfer-Encoding' => 'binary',
    'Content-Description' => 'File Transfer',
]

Note that the default charset and encoding are automatically overwrited if a custom encoding is specified in the options (see below).

Options

The last argument lets you define how the CSV is built and formated. We have defined a format that fits very well in most cases but the optimal configuration may depend on your environment (language, Microsoft Office version, etc.).

The default options are the following.

[
    'encoding' => 'WINDOWS-1252',
    'delimiter' => ';',
    'quoted' => true,
    'include_header' => true,
]

neoxia/laravel-csv-response 适用场景与选型建议

neoxia/laravel-csv-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 52.45k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2017 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 neoxia/laravel-csv-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 neoxia/laravel-csv-response 我们能提供哪些服务?
定制开发 / 二次开发

基于 neoxia/laravel-csv-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 52.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 32
  • Forks: 20
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-09