Skip to content

Latest commit

 

History

History
124 lines (95 loc) · 3.38 KB

4-dynamic-routes-usage.md

File metadata and controls

124 lines (95 loc) · 3.38 KB

Dynamic routes usage

You can also register event listeners (or subscribers) to populate your sitemap(s).

Imagine that your application is (or has) a blog, and that you want to add to your sitemap all blog posts that your administrator has created.

Note: We choose an event subscriber as example, but you can also do it with an event listener.

If you are not familiar with the concept of event listener/subscriber/dispatcher, please have a look to Symfony's official documentation.

EventListener class

<?php

namespace App\EventListener;

use App\Repository\BlogPostRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;

class SitemapSubscriber implements EventSubscriberInterface
{
    /**
     * @var BlogPostRepository
     */
    private $blogPostRepository;

    /**
     * @param BlogPostRepository $blogPostRepository
     */
    public function __construct(BlogPostRepository $blogPostRepository)
    {
        $this->blogPostRepository = $blogPostRepository;
    }

    /**
     * @inheritdoc
     */
    public static function getSubscribedEvents()
    {
        return [
            SitemapPopulateEvent::class => 'populate',
        ];
    }

    /**
     * @param SitemapPopulateEvent $event
     */
    public function populate(SitemapPopulateEvent $event): void
    {
        $this->registerBlogPostsUrls($event->getUrlContainer(), $event->getUrlGenerator());
    }

    /**
     * @param UrlContainerInterface $urls
     * @param UrlGeneratorInterface $router
     */
    public function registerBlogPostsUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router): void
    {
        $posts = $this->blogPostRepository->findAll();

        foreach ($posts as $post) {
            $urls->addUrl(
                new UrlConcrete(
                    $router->generate(
                        'blog_post',
                        ['slug' => $post->getSlug()],
                        UrlGeneratorInterface::ABSOLUTE_URL
                    )
                ),
                'blog'
            );
        }
    }
}

Note: you should not use this snippet as is. With large dataset, findAll is not a good idea. Please read Doctrine documentation, to learn about iterator and array hydrate.

Service definition

If you are using PSR4 service discovery, your event listener is already registered. Otherwhise you will have to register it by hand.

Using XML

<services>
    <service id="app.sitemap.blog_post_subscriber" class="App\EventListener\SitemapSubscriber">
        <argument type="service" id="<your repository service id>"/>
        <tag name="kernel.event_subscriber" priority="100"/>
    </service>
</services>

Using YAML

services:
    app.sitemap.blog_post_subscriber:
        class: App\EventListener\SitemapSubscriber
        arguments:
            - "@<your repository service id>"
        tags:
            - { name: "kernel.event_subscriber", priority: 100 }

Note: Choosing a priority for your event listener is up to you.


« Static routes usageDecorating URLs »