I have the following lines of code:
$objPHPExcel = new PHPExcel();
..ADDITIONAL CODE..
// Redirect output to a client.s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="powers-id_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
When this code is executed from a PHP file (outside of a class) the Excel file is created and saved to the browser's download directory.
I then moved all of the code to it's own class:
class ReportController extends BaseController {
function generate() {
$objPHPExcel = new PHPExcel();
..ADDITIONAL CODE..
// Redirect output to a client.s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="powers-id_report.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
$this->fw->reroute('/');
}
}
And it fails. I am using Fat Free PHP to redirect the request to this class. I've used break points to see that it is still executing all of the code the same as before but no file is being outputted. The $objWriter->save('php://output');
is returning true
which should mean it successfully saved the file. I have also tried removing the reroute. My error logs are all empty as well.
The solution is
$objWriter->save('php://output');
die();
This prevents execution of $f3
's controller functions such as afterroute
which may change headers.