Currently Yii offers several formatters like XML, JSON, JSONP.
It would be nice if Yii supported CSV as well.
This implementation uses a stream to support large data sets. (Using it with large data sets would require a traversable object to be passed in data, not a PHP array).
/**
* CsvResponseFormatter formats the given data into a csv response content.
*
* It is used by [[Response]] to format response data.
*
*/
class CsvResponseFormatter extends Component implements ResponseFormatterInterface
{
/**
* If true column names will be printed as the first line, if provided.
*/
public $includeColumnNames = true;
/**
* Formats the specified response.
* @param Response $response the response to be formatted.
*/
public function format($response)
{
$response->getHeaders()->set('Content-Type', 'text/csv; charset=UTF-8');
$data = $response->data;
$handle = fopen('php://temp/maxmemory:20971520', 'w+');
$first = true;
foreach($data as $row) {
if($first && $this->includeColumnNames && \yii\helpers\ArrayHelper::isAssociative($row)) {
fputcsv($handle, array_keys($row));
$first = false;
}
fputcsv($handle, $row);
}
rewind($handle);
$response->stream = $handle;
}
}
@yiisoft/core-developers any objections?
Shouldn't this formatter also support all the CSV options for fputcsv?
delimiter
The optional delimiter parameter sets the field delimiter (one character only).enclosure
The optional enclosure parameter sets the field enclosure (one character only).escape_char
The optional escape_char parameter sets the escape character (one character only).
Yes.
@samdark, does self-assigning mean that you are implementing it or just that you are in charge of managing it? --> I can implement it if you want.
Just in charge. I'd assign you but you're not in the yiisoft ;)
Have also added support for non-uniform data, for example an array of models where one model has columns A and B, and another model has A and C.
Tests have been added & changelog has been updated.
My opinion: this feature does not belongs to the core and it is better to be released and maintained as separated extension.
Although CSV eneration is quite common task indeed, I don't think it is a good idea to keep it inside the core. There are many nuances about the task itself and many possible ways of resolving it.
If we accept this - what next? 'PDF', 'RSS', 'YAML' - it will be no end to this.
Currently beside 'raw' and 'html' Yii supports only those formats, which are common for REST API. For me - that is enough. Everything else - is an application specific.
I don't agree; but if that's the teams' choice feel free to close the issue and I'll put it my extension repository instead.
I also think this is better suited as an extension.
nice feature, i would love to try this on our current application
OK. So let's do it as extension. @cebe, @klimov-paul is it general enough for extension under yiisoft?
is it general enough for extension under yiisoft?
I suppose @SamMousa is capable to maintain this extension on this own. Beside, since it is fully his idea it would be better, if he will use his own brand for it.
Ok. Thanks for the feedback, will set this up as separate extension.
On Jun 8, 2016 7:14 PM, "Paul Klimov" [email protected] wrote:
is it general enough for extension under yiisoft?
I suppose @SamMousa https://github.com/SamMousa is capable to maintain
this extension on this own. Beside, since it is fully his idea it would be
better, if he will use his own brand for it.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/yiisoft/yii2/issues/11674#issuecomment-224662026, or mute
the thread
https://github.com/notifications/unsubscribe/AAhYzRUEPvSgMTG3CqYb2QIAHjuFw_r2ks5qJvhagaJpZM4Irk0g
.
Most helpful comment
My opinion: this feature does not belongs to the core and it is better to be released and maintained as separated extension.
Although CSV eneration is quite common task indeed, I don't think it is a good idea to keep it inside the core. There are many nuances about the task itself and many possible ways of resolving it.
If we accept this - what next? 'PDF', 'RSS', 'YAML' - it will be no end to this.
Currently beside 'raw' and 'html' Yii supports only those formats, which are common for REST API. For me - that is enough. Everything else - is an application specific.