Route with CSV download adds "Not Found" (404 error) into the CSV file

Dear Kirby community,

for a Kirby 3 site, I use this router to export some registration data as CSV file.

        [
      'pattern' => 'api/csv',
      'action' => function() {
        header::download(['mime'=>'application/csv', 'name' => 'regs.csv', 'Pragma' => 'no-cache', 'Content-Disposition' => 'attachment']);
        $regs = site()->index()->find('register/anmeldestatus')->anmeldungen()->yaml();
        $f = fopen('php://output', 'w');
        foreach($regs as $registration) {
            fputcsv($f, $registration, ';','"');
        }
        fpassthru($f);
        return false;
      }
    ]
  ]

The file download works, the CSV file is well formed, but in the last line it contains the words “Not found” or the HTML of an error page, when there is one.

Any idea here on the forum?

Many thanks in advance

Just guessing, but maybe because your return false.

Ah, thanks, yes. Seems, I should add a “new Response”.

Just wonder, what would be the right type for a CSV download…

@texnixe Thank you very much for nudging me in the right direction, replaced my previous response with

return new Response($f, 'application/csv');

Works as expected!