src/Controller/SeoController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class SeoController extends AbstractController
  10. {
  11.     #[Route('robots.txt'name'sitemap')]
  12.     public function robots(): Response
  13.     {
  14.         $response = new Response();
  15.         $response->headers->set('Content-Type''text/plain');
  16.         return $this->render('robots.txt.twig'response$response);
  17.     }
  18.     #[Route(path: [
  19.         'en_GB' => '/en/sitemap.xml',
  20.         'fr_FR' => '/fr/sitemap.xml',
  21.     ], name'sitemap')]
  22.     public function sitemap(Request $requestParameterBagInterface $bag): BinaryFileResponse
  23.     {
  24.         $file sprintf('%s/public/sitemap-%s.xml'$bag->get('kernel.project_dir'), $request->getLocale());
  25.         if (!file_exists($file)) {
  26.             throw $this->createNotFoundException();
  27.         }
  28.         return new BinaryFileResponse($file);
  29.     }
  30. }