src/EventListener/SitemapSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Repository\VehiculeRepository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Presta\SitemapBundle\Event\SitemapPopulateEvent;
  7. use Presta\SitemapBundle\Service\UrlContainerInterface;
  8. use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
  9. class SitemapSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var UrlGeneratorInterface
  13.      */
  14.     private $urlGenerator;
  15.     /**
  16.      * @var VehiculeRepository
  17.      */
  18.     private $VehiculeRepository;
  19.     /**
  20.      * @param UrlGeneratorInterface $urlGenerator
  21.      * @param VehiculeRepository    $VehiculeRepository
  22.      */
  23.     public function __construct(UrlGeneratorInterface $urlGeneratorVehiculeRepository $VehiculeRepository)
  24.     {
  25.         $this->urlGenerator $urlGenerator;
  26.         $this->VehiculeRepository $VehiculeRepository;
  27.     }
  28.     /**
  29.      * @inheritdoc
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             SitemapPopulateEvent::ON_SITEMAP_POPULATE => 'populate',
  35.         ];
  36.     }
  37.     /**
  38.      * @param SitemapPopulateEvent $event
  39.      */
  40.     public function populate(SitemapPopulateEvent $event): void
  41.     {
  42.         $this->registerBlogPostsUrls($event->getUrlContainer());
  43.     }
  44.     /**
  45.      * @param UrlContainerInterface $urls
  46.      */
  47.     public function registerBlogPostsUrls(UrlContainerInterface $urls): void
  48.     {
  49.         $posts $this->VehiculeRepository->findAll();
  50.         foreach ($posts as $post) {
  51.             $urls->addUrl(
  52.                 new UrlConcrete(
  53.                     $this->urlGenerator->generate(
  54.                         'fiche_produit',
  55.                         ['slug' => $post->getUrl()],
  56.                         UrlGeneratorInterface::ABSOLUTE_URL
  57.                     )
  58.                 ),
  59.                 'fiche_produit'
  60.             );
  61.         }
  62.     }
  63.     
  64. }