Reputation: 57
i'm calling a controller function from a form and redirecting back to the same page. All i want the page to be refreshed so that flash message can be displayed. it's not redirecting to the page instead it stays on the page and calls function!
Here's the code:
public function triggerDownload($DownData , $filename){
Yii::import('application.extensions.phpexcel.Classes.PHPExcel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$rowCount = 1;
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, 'USER NAME');
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, 'EMAIL ADDRESS');
if (array_key_exists("cam_id",$DownData)){
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, 'CAM TITLE');
}
$rowCount++;
foreach ($DownData as $key => $value) {
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $value['username']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $value['email']);
if (array_key_exists("cam_id",$DownData)){
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $value['cam_id']);
}
$rowCount++;
}
//make first row bold
$objPHPExcel->getActiveSheet()->getStyle("A1:I1")->getFont()->setBold(true);
$objPHPExcel->setActiveSheetIndex(0);
for($col = 'A'; $col !== 'G'; $col++) {
$objPHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
}
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".$filename.".xlsx\"");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("php://output");
Yii::app()->user->setFlash('success', 'Report Downloaded Successfully!');
return $this->redirect('/admin/');
}
Upvotes: 1
Views: 548
Reputation: 61
You need to use the following typing:
return $this->redirect(['admin']);
And if you are trying that in beforeAction() you should use send() method
return $this->redirect(['admin'],302)->send();
Upvotes: 1
Reputation: 868
Instead of
return $this->redirect('/admin/');
Try
$this->redirect(Yii::app()->request->urlReferrer);
(no return
needed)
Upvotes: 1