src/Controller/SitemapController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Exception;
  4. use Pimcore\Model\Document;
  5. use Pimcore\Model\Document\Page;
  6. use Pimcore\Model\Site;
  7. use Pimcore\Model\DataObject;
  8. use Pimcore\Tool;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. class SitemapController extends AbstractController
  15. {
  16.     // --------------------------------
  17.     // VARIABLES
  18.     // --------------------------------
  19.     private $sitemapElements = [];
  20.     // --------------------------------
  21.     // SITEMAP
  22.     // --------------------------------
  23.     /**
  24.      * @Route("/sitemap.xml", name="sitemap")
  25.      * 
  26.      * @return Response
  27.      */
  28.     public function xmlAction(TranslatorInterface $translator)
  29.     {
  30.         try {
  31.             $rootDoc Document::getByPath('/');
  32.             $baseUrl Tool::getHostUrl();
  33.             if (Site::isSiteRequest()) {
  34.                 $current Site::getCurrentSite();
  35.                 $rootDoc $current->getRootDocument();
  36.             }
  37.             $this->recursiveSitemap($rootDoc$baseUrl);
  38.             // detail pages
  39.             // $this->newsDetails($translator, $baseUrl);
  40.             array_unshift($this->sitemapElements'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
  41.             array_unshift($this->sitemapElements'<?xml version="1.0" encoding="UTF-8" ?>');
  42.             array_push($this->sitemapElements'</urlset>');
  43.             return new Response(trim(implode(PHP_EOL$this->sitemapElements)), 200, ['Content-Type' => 'text/xml']);
  44.         } catch (Exception $e) {
  45.             return new Response(''404);
  46.         }
  47.     }
  48.     // --------------------------------
  49.     // RECURSIVE SITEMAP
  50.     // --------------------------------
  51.     /**
  52.      * @param Document $doc
  53.      * @param $baseUrl
  54.      */
  55.     private function recursiveSitemap(Document $doc$baseUrl)
  56.     {
  57.         if ($doc->hasChildren()) {
  58.             foreach ($doc->getChildren() as $child) {
  59.                 if ($child instanceof Document) {
  60.                     
  61.                     if ($child->hasProperty('sitemap_exclude') && $child->getProperty('sitemap_exclude')) {
  62.                         continue;
  63.                     }
  64.                     if (!$child->isPublished()) {
  65.                         continue;
  66.                     }
  67.                     if ($child instanceof Page) {
  68.                         array_push($this->sitemapElements'<url><loc>' $baseUrl $child->getFullPath() . '</loc><lastmod>' date('Y-m-d'$child->getModificationDate()) . '</lastmod></url>');
  69.                     }
  70.                     $this->recursiveSitemap($child$baseUrl);
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     // --------------------------------
  76.     // NEWS DETAIL PAGES
  77.     // --------------------------------
  78.     /**
  79.      * @param $baseUrl
  80.      */
  81.     /*private function newsDetails($translator, $baseUrl) {
  82.         // objects
  83.         $list = new DataObject\News\Listing();
  84.         $list->setOrderKey('date');
  85.         $list->setOrder('desc');
  86.         $list->load();
  87.         foreach ($list as $detail) {
  88.             // languages
  89.             $siteLanguages = \Pimcore\Tool::getValidLanguages();
  90.             // last modification date
  91.             $detailLastMod = $detail->getModificationDate();
  92.             foreach ($siteLanguages as $siteLanguage) {
  93.                 $subPath = $translator->trans(
  94.                     'news-detail-route-path',
  95.                     [],
  96.                     'messages',
  97.                     $siteLanguage
  98.                 );
  99.                 $detailLink = $baseUrl . '/' . $siteLanguage . '/' . $subPath . '/' . HelperController::toUri($detail->getTitle($siteLanguage)) . '_n' . $detail->getId();
  100.                 // add link to sitemap
  101.                 array_push($this->sitemapElements, '<url><loc>' . $detailLink . '</loc><lastmod>' . date('Y-m-d', $detailLastMod) . '</lastmod></url>');
  102.                 
  103.             }
  104.         }
  105.     }*/
  106. }