CsvStrategy extension for zendframework/zend-view
- Configure the CsvStrategy to the ViewManager in
module.config.php
<?php return [ 'view_manager' => [ 'template_path_stack' => [ __DIR__ . '/../view', ], 'template_map' => [ 'export/csv' => __DIR__.'/../view/refuels/index/export.pcsv' ], 'strategies' => [ View\Strategy\CsvStrategy::class ] ] ]
- Create csv view file as
export.pcsv
<?php foreach($this->data as $row) { echo implode($row, ';').PHP_EOL; }
- Use it in controller actions
<?php namespace Test; use LegoW\View\Model\CsvModel; class TestController extends AbstractActionController { public function indexAction() { $view = new CsvModel(); $view->setTerminate(true) ->setVariables([ "data" => [ range(1,26), range('a','z') ] ]); return $view; } }