src/Controller/SitemapController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Expert;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class SitemapController extends AbstractController
  10. {
  11.     /**
  12.      * @Route("/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
  13.      */
  14.     public function index(
  15.         Request $request,
  16.         EntityManagerInterface $em
  17.     ): Response {
  18.         $hostname $request->getSchemeAndHttpHost();
  19.         $urls = [];
  20.         $urls[] = ['loc' => $this->generateUrl('app_home')];
  21.         $urls[] = ['loc' => $this->generateUrl('app_expert_index')];
  22.         $urls[] = ['loc' => $this->generateUrl('app_difference')];
  23.         $urls[] = ['loc' => $this->generateUrl('app_office')];
  24.         $urls[] = ['loc' => $this->generateUrl('app_tarif')];
  25.         $urls[] = ['loc' => $this->generateUrl('app_contact')];
  26.         $urls[] = ['loc' => $this->generateUrl('app_mentions_legales')];
  27.         // experts list
  28.         foreach ($em->getRepository(Expert::class)->findAll() as $expert) {
  29.             $images = [
  30.                 'loc' => '/assets/img/experts/' $expert->getImage(),
  31.                 'title' => $expert->getFirstname() . ' ' $expert->getLastname()
  32.             ];
  33.             $urls[] = [
  34.                 'loc' => $this->generateUrl('app_expert_show', [
  35.                     'slug' => $expert->getSlug(),
  36.                 ]),
  37.                 'image' => $images
  38.             ];
  39.         }
  40.         // return $this->render('sitemap/index.html.twig', [
  41.         //     'controller_name' => 'SitemapController',
  42.         // ]);
  43.         $response = new Response(
  44.             $this->renderView('sitemap/index.html.twig', [
  45.                 'urls' => $urls,
  46.                 'hostname' => $hostname]
  47.             )
  48.         );
  49.         $response->headers->set('Content-Type''text/xml');
  50.         return $response;
  51.     }
  52. }