src/Controller/SitemapController.php line 23
<?phpnamespace App\Controller;use App\Enum\SiteMapEnum;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;class SitemapController extends AbstractController{private EntityManagerInterface $entityManager;public function __construct(EntityManagerInterface $entityManager){$this->entityManager = $entityManager;}#[Route('/sitemap.xml', name: 'sitemap', defaults: ['_format' => 'xml'])]public function index(): Response{$urls = [];// Add static routesforeach (SiteMapEnum::SITE_ROUTES as $item){$url =['loc' => $this->generateUrl($item, ['_locale' => 'hy'], UrlGeneratorInterface::ABSOLUTE_URL),'lastmod' => new \DateTime(),'changefreq' => 'daily','priority' => 1.0,'alternates' => [['hreflang' => 'hy', 'href' => $this->generateUrl($item, ['_locale' => 'hy'], UrlGeneratorInterface::ABSOLUTE_URL)],['hreflang' => 'en', 'href' => $this->generateUrl($item, ['_locale' => 'en'], UrlGeneratorInterface::ABSOLUTE_URL)],['hreflang' => 'ru', 'href' => $this->generateUrl($item, ['_locale' => 'ru'], UrlGeneratorInterface::ABSOLUTE_URL)],]];$urls[] = $url;}foreach (SiteMapEnum::SITE_DYNAMIC_ROUTES as $item){$posts = $this->entityManager->getRepository($item['class'])->findAll();foreach ($posts as $post) {$routeParams = $item['route'] === 'page_ticket'? ['id' => $post->getId()]: ['slug' => $post->getSlug()];$urls[] = ['loc' => $this->generateUrl($item['route'], array_merge(['_locale' => 'hy'], $routeParams), UrlGeneratorInterface::ABSOLUTE_URL),'lastmod' => new \DateTime(),'changefreq' => 'daily','priority' => 1.0,'alternates' => [['hreflang' => 'hy', 'href' => $this->generateUrl($item['route'], array_merge(['_locale' => 'hy'], $routeParams), UrlGeneratorInterface::ABSOLUTE_URL)],['hreflang' => 'en', 'href' => $this->generateUrl($item['route'], array_merge(['_locale' => 'en'], $routeParams), UrlGeneratorInterface::ABSOLUTE_URL)],['hreflang' => 'ru', 'href' => $this->generateUrl($item['route'], array_merge(['_locale' => 'ru'], $routeParams), UrlGeneratorInterface::ABSOLUTE_URL)],]];}}$response = $this->render('sitemap/index.xml.twig', ['urls' => $urls,]);$response->headers->set('Content-Type', 'application/xml');return $response;}}