before(function() use ($app) { $app['firewall']->requireAuthentication(); $app['firewall']->requireAccessToModule('report'); }); $controllers->post('/csv', $this->call('exportCSV')) ->bind('report_export_csv'); return $controllers; } /** * Export data to a csv file * * @param Application $app * @param Request $request * @return Response */ public function exportCSV(Application $app, Request $request) { $name = $request->request->get('name', 'export'); if (null === $data = $request->request->get('csv')) { $app->abort(400); } $filename = mb_strtolower('report_' . $name . '_' . date('dmY') . '.csv'); $data = preg_replace('/[ \t\r\f]+/', '', $data); $response = new Response($data, 200, array( 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate('D, d M Y H:i:s'). ' GMT', 'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Cache-Control' => 'post-check=0, pre-check=0', 'Pragma' => 'no-cache', 'Content-Type' => 'text/csv', 'Content-Length' => strlen($data), 'Cache-Control' => 'max-age=3600, must-revalidate', 'Content-Disposition' => 'max-age=3600, must-revalidate', )); $response->headers->set('Content-Disposition', $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename)); return $response; } /** * Prefix the method to call with the controller class name * * @param string $method The method to call * @return string */ private function call($method) { return sprintf('%s::%s', __CLASS__, $method); } }