src/Controller/DownloadsController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Downloads;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class DownloadsController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/download/{download_url}", name="download_url")
  14.      */
  15.     public function downloadFileAction(Request $request): Response
  16.     {
  17.         $url $request->get('download_url');
  18.         $path __DIR__ '/../../public/documents/';
  19.         $pdf $this->getDoctrine()->getRepository(Downloads::class)->findOneBy(['url' => $url]);
  20.         $response = new BinaryFileResponse($path $pdf->getFile());
  21.         $response->setContentDisposition(
  22.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  23.             $pdf->getFile()
  24.         );
  25.         return $response;
  26.     }
  27. }