How to create a link that download a file in Symfony How to create a link that download a file in Symfony symfony symfony

How to create a link that download a file in Symfony


Set up an action.This uses annotation for the route. YOu can of course use yml or xml or whatever you are currently using

/** * @Route("/download", name="download_file")**/public function downloadFileAction(){    $response = new BinaryFileResponse('path/to/pdf.pdf');    $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT,'pdf.pdf');    return $response;}

Twig template:

<a href="{{path('download_file')}}">Download file</a>