DEV

fuelphp - csv出力

参考:FuelPHPでCSVの出力

app/classes/format.php を作成

上記参考サイトどおりにやる

<?php
class Format extends Fuel\Core\Format {
/**
* CSV出力をSJIS-WINで返す
* @access public
* @param mixed $data
* @return string csv(sjis-win)
*/
public function to_csv($data = null, $delimiter = null){
$csv = parent::to_csv($data, $delimiter);
$csv = mb_convert_encoding($csv, 'SJIS-win', 'UTF-8');
return $csv;
}
}

app/bootstrap.php の一部を修正

上記参考サイトどおりにやる

Autoloader::add_classes(array(
// Add classes you want to override here
// Example: 'View' => APPPATH.'classes/view.php',
'Validation_Error' => APPPATH.'classes/validation/error.php',
'Format' => APPPATH . 'classes/format.php',
));

コントローラーにPDF出力アクションを追加

例えばcompanyコントローラーにaction_pdfを追加する

/**
* 事業者情報のPDFダウンロード
*/
public function action_pdf(){
$data = array(
array('会社名', '郵便番号', '住所', '代表者名', 'URL'),
array('Logicky', '123', '東京都', 'きむたく', 'http://google.com'),
array('電通', '123', '東京都', 'つまぶききん', 'http://google.co.jp'),
);
$this->template = null;
$this->response = new Response();
$this->response->set_header('Content-Type', 'application/csv');
$this->response->set_header('Content-Disposition', 'attachment; filename="company.csv"');
$this->response->send(true);
echo Format::forge($data)->to_csv();
return;
}