src/Controller/SitemapController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\BienRepository;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. class SitemapController extends AbstractController
  9. {
  10.     /**
  11.      * @Route("/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
  12.      */
  13.     public function index(
  14.         Request $request,
  15.         BienRepository $bienRepository
  16.     ): Response {
  17.         $hostname = $request->getSchemeAndHttpHost();
  18.         $urls = [];
  19.         $urls[] = ['loc'    => $this->generateUrl('app_about')];
  20.         $urls[] = ['loc'    => $this->generateUrl('app_biens_index')];
  21.         $urls[] = ['loc'    => $this->generateUrl('app_home')];
  22.         $urls[] = ['loc'    => $this->generateUrl('app_contact')];
  23.         $urls[] = ['loc'    => $this->generateUrl('app_equipe')];
  24.         $urls[] = ['loc'    => $this->generateUrl('app_honoraires')];
  25.         $urls[] = ['loc'    => $this->generateUrl('app_estimation')];
  26.         foreach($bienRepository->findAll() as $bien) {
  27.             $urls[] = [
  28.                 'loc'       => $this->generateUrl('app_biens_show', ['id' => $bien->getId()]),
  29.                 'lastmod'   => (new \DateTime())->format('Y-m-d')
  30.             ];
  31.         }
  32.         $response = new Response(
  33.             $this->renderView('sitemap/index.html.twig', [
  34.                 'urls'      => $urls,
  35.                 'hostname'  => $hostname,
  36.             ]),
  37.             200
  38.         );
  39.         $response->headers->set('Content-type', 'text/xml');
  40.         return $response;
  41.     }
  42. }