src/Controller/SitemapController.php line 23

  1. <?php
  2. namespace App\Controller;
  3. use App\Enum\SiteMapEnum;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. class SitemapController extends AbstractController
  10. {
  11.     private EntityManagerInterface $entityManager;
  12.     public function __construct(EntityManagerInterface $entityManager)
  13.     {
  14.         $this->entityManager $entityManager;
  15.     }
  16.     #[Route('/sitemap.xml'name'sitemap'defaults: ['_format' => 'xml'])]
  17.     public function index(): Response
  18.     {
  19.         $urls = [];
  20.         // Add static routes
  21.         foreach (SiteMapEnum::SITE_ROUTES as $item){
  22.             $url =
  23.                 [
  24.                     'loc' => $this->generateUrl($item, ['_locale' => 'hy'], UrlGeneratorInterface::ABSOLUTE_URL),
  25.                     'lastmod' =>  new \DateTime(),
  26.                     'changefreq' => 'daily',
  27.                     'priority' => 1.0,
  28.                     'alternates' => [
  29.                         ['hreflang' => 'hy''href' => $this->generateUrl($item, ['_locale' => 'hy'], UrlGeneratorInterface::ABSOLUTE_URL)],
  30.                         ['hreflang' => 'en''href' => $this->generateUrl($item, ['_locale' => 'en'], UrlGeneratorInterface::ABSOLUTE_URL)],
  31.                         ['hreflang' => 'ru''href' => $this->generateUrl($item, ['_locale' => 'ru'], UrlGeneratorInterface::ABSOLUTE_URL)],
  32.                     ]
  33.                 ];
  34.             $urls[] = $url;
  35.         }
  36.         foreach (SiteMapEnum::SITE_DYNAMIC_ROUTES as $item){
  37.             $posts $this->entityManager->getRepository($item['class'])->findAll();
  38.             foreach ($posts as $post) {
  39.                 $routeParams $item['route'] === 'page_ticket'
  40.                     ? ['id' => $post->getId()]
  41.                     : ['slug' => $post->getSlug()];
  42.                 $urls[] = [
  43.                     'loc' => $this->generateUrl($item['route'], array_merge(['_locale' => 'hy'], $routeParams), UrlGeneratorInterface::ABSOLUTE_URL),
  44.                     'lastmod' =>  new \DateTime(),
  45.                     'changefreq' => 'daily',
  46.                     'priority' => 1.0,
  47.                     'alternates' => [
  48.                         ['hreflang' => 'hy''href' => $this->generateUrl($item['route'], array_merge(['_locale' => 'hy'], $routeParams),  UrlGeneratorInterface::ABSOLUTE_URL)],
  49.                         ['hreflang' => 'en''href' => $this->generateUrl($item['route'], array_merge(['_locale' => 'en'], $routeParams),  UrlGeneratorInterface::ABSOLUTE_URL)],
  50.                         ['hreflang' => 'ru''href' => $this->generateUrl($item['route'], array_merge(['_locale' => 'ru'], $routeParams),  UrlGeneratorInterface::ABSOLUTE_URL)],
  51.                     ]
  52.                 ];
  53.             }
  54.         }
  55.         $response $this->render('sitemap/index.xml.twig', [
  56.             'urls' => $urls,
  57.         ]);
  58.         $response->headers->set('Content-Type''application/xml');
  59.         return $response;
  60.     }
  61. }