vendor/kunstmaan/sitemap-bundle/Controller/SitemapController.php line 63

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\SitemapBundle\Controller;
  3. use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
  4. use Kunstmaan\NodeBundle\Helper\NodeMenu;
  5. use Kunstmaan\SitemapBundle\Event\PreSitemapIndexRenderEvent;
  6. use Kunstmaan\SitemapBundle\Event\PreSitemapRenderEvent;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  12. final class SitemapController extends AbstractController
  13. {
  14.     /** @var NodeMenu */
  15.     private $nodeMenu;
  16.     /** @var DomainConfigurationInterface */
  17.     private $domainConfiguration;
  18.     /** @var EventDispatcherInterface */
  19.     private $eventDispatcher;
  20.     public function __construct(NodeMenu $nodeMenuDomainConfigurationInterface $domainConfigurationEventDispatcherInterface $eventDispatcher)
  21.     {
  22.         $this->nodeMenu $nodeMenu;
  23.         $this->domainConfiguration $domainConfiguration;
  24.         $this->eventDispatcher $eventDispatcher;
  25.     }
  26.     /**
  27.      * This will generate a sitemap for the specified locale.
  28.      * Use the mode parameter to select in which mode the sitemap should be
  29.      * generated. At this moment only XML is supported
  30.      */
  31.     #[Route(path'/sitemap-{locale}.{_format}'name'KunstmaanSitemapBundle_sitemap'requirements: ['_format' => 'xml'])]
  32.     public function sitemapAction($locale): Response
  33.     {
  34.         $nodeMenu $this->nodeMenu;
  35.         $nodeMenu->setLocale($locale);
  36.         $nodeMenu->setIncludeOffline(false);
  37.         $nodeMenu->setIncludeHiddenFromNav(true);
  38.         $nodeMenu->setCurrentNode(null);
  39.         $event = new PreSitemapRenderEvent($locale);
  40.         $this->eventDispatcher->dispatch($eventPreSitemapRenderEvent::NAME);
  41.         return $this->render('@KunstmaanSitemap/Sitemap/view.xml.twig', [
  42.             'nodemenu' => $nodeMenu,
  43.             'locale' => $locale,
  44.             'extraItems' => $event->getExtraItems(),
  45.         ]);
  46.     }
  47.     /**
  48.      * This will generate a sitemap index file to define a sub sitemap for each
  49.      * language. Info at:
  50.      * https://support.google.com/webmasters/answer/75712?rd=1 Use the mode
  51.      * parameter to select in which mode the sitemap should be generated. At
  52.      * this moment only XML is supported
  53.      */
  54.     #[Route(path'/sitemap.{_format}'name'KunstmaanSitemapBundle_sitemapindex'requirements: ['_format' => 'xml'])]
  55.     public function sitemapIndexAction(Request $request): Response
  56.     {
  57.         $locales $this->domainConfiguration->getBackendLocales();
  58.         $event = new PreSitemapIndexRenderEvent($locales);
  59.         $this->eventDispatcher->dispatch($eventPreSitemapIndexRenderEvent::NAME);
  60.         return $this->render('@KunstmaanSitemap/SitemapIndex/view.xml.twig', [
  61.             'locales' => $locales,
  62.             'host' => $request->getSchemeAndHttpHost(),
  63.             'extraSitemaps' => $event->getExtraSitemaps(),
  64.         ]);
  65.     }
  66. }