'csv'])] public function socialIssueList(Request $request, string $_format = 'csv'): StreamedResponse { if (!$this->security->isGranted('ROLE_ADMIN')) { throw new AccessDeniedHttpException('Only ROLE_ADMIN can export this list'); } $streamCallback = function () use ($request) { $output = fopen('php://output', 'w'); // Add BOM for UTF-8 fwrite($output, "\xEF\xBB\xBF"); // Create CSV writer $csv = Writer::createFromStream($output); // Write header row $header = array_map( fn (string $e) => $this->translator->trans($e), [ 'Id', 'Kind', 'Civility', 'Firstname', 'Name', 'Profession', 'Telephone', 'Telephone2', 'Email', 'Address', 'Comment', 'Parent', 'Name_company', 'Acronym', 'Active', 'Contact id', 'Contact name', 'Contact firstname', 'Contact phone', 'Contact phone2', 'Contact email', 'Contact address', 'Contact profession', ] ); $csv->insertOne($header); foreach ($this->thirdPartyRepository->findThirdpartiesWithContacts($request->getLocale()) as $row) { $csv->insertOne($row); flush(); } }; return new StreamedResponse( function () use ($streamCallback) { $streamCallback(); }, Response::HTTP_OK, [ 'Content-Encoding' => 'none', 'Content-Type' => 'text/csv; charset=UTF-8', 'Content-Disposition' => 'attachment; filename="thirdparties.csv"', ] ); } }