Skip to content
SitemapBundle Reloaded
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


This SitemapBundle requires DoctrineMongoDBBundle enabled with default document manager available.

In your doctrine mongodb odm bundle configuration add the following, in order to register sitemap documents metadata:

    # some odm configuration
        # other bundles mapping and etc.
            type: yml
            prefix: Avalanche\Bundle\SitemapBundle\Sitemap

Installation as a submodule

The most common way to install a Bundle is by adding it as a submodule to your existing project. This setup will let you update the Bundle with ease in the future.

From the root directory of your Symfony2 application issue the following commands: $> mkdir src/Bundle/Avalanche $> git submodule add git:// src/Avalanche/Bundle/SitemapBundle

Adding bundle to Kernel

This bundle will help with sitemap generation in your Symfony2 based projects. To enable the sitemap bundle, add it to you kernel registerBundles() method:

use Symfony\Foundation\Kernel;

class MyKernel extends Kernel {
    public function registerBundles() {
        return array(
            new Avalanche\Bundle\SitemapBundle\AvalancheSitemapBundle(),

    public function registerBundleDirs()
        return array(
            'Avalanche' => __DIR__.'/../src/Avalanche',

Unique indexes

Each entry in a sitemap (url or image) must have a unique loc attribute. AvalancheSitemapBundle defines indexes in the doctrine odm metadata for url class. To create them in your mongodb database, run the following command:

$> app/console doctrine:mongodb:schema:create --index

Enabling the services

The second step is to enable its DependencyInjection extension in your config.yml:

    base_url: ""

To store urls in a custom collection or database, use the following:

    base_url: ""
    database: some_database
    collection: urls

By default 'sitemap' database will be used, with unique urls collection per Kernel.

Writing custom url providers for sitemap:generate command

The third step is to write your url providers to populate the 'sitemap' with existing urls, e.g:


namespace My\ForumBundle\Sitemap;

use Avalanche\Bundle\SitemapBundle\Sitemap\Provider;
use Avalanche\Bundle\SitemapBundle\Sitemap\Sitemap;
use Avalanche\Bundle\SitemapBundle\Sitemap\Sitemap\Url;
use Avalanche\Bundle\SitemapBundle\Sitemap\Sitemap\Url\Image;
use Symfony\Component\Routing\Router;
use My\ForumBundle\Document\TopicRepository;

class ForumTopicProvider implements SitemapProvider {

    protected $topicRepository;
    protected $router;

    public function __construct(TopicRepository $topicRepository, Router $router)
        $this->topicRepository = $topicRepository;
        $this->router = $router;

    public function populate(Sitemap $sitemap)
        foreach ($this->topicRepository->find() as $topic) {
            $url = new Url($this->router->generate('topic_view', array('id' => $topic->getId()));

            foreach ($topic->getAttachedImages() as $attachedImage) {
                $image = new Image($this->router->generate('topic_image_view', array('id' => $attachedImage->getId())));




NOTE: in the above example, we use router to relative urls or paths. Upon rendering, sitemap will figure out if the url is relative and will prefix it with current base url. If you want your urls to belong to certain domain, that might be different from the one the sitemap will be available at, make sure to use absolute urls.

And register your provider in DIC like this:

<service id="forum.sitemap.provider" class="My\ForumBundle\ForumTopicProvider">
    <tag name="sitemap.provider" />
    <argument type="service" id="forum.document_repository.topic" />
    <argument type="service" id="router" />

After providers are in place and registered, time to run the generation command:

> php forum/console sitemap:generate

or simply:

> php forum/console sitemap:g

Creating/Updating sitemap urls in the application

Creating/updating urls from web is as easy as from cli:

$url = $this->sitemap->get('/topics/1');
$url->setLastmod(new \DateTime());

$this->sitemap->add(new Url(''));


Enabling sitemap routes

The last and most important step is to enable sitemap routing in your routing.yml:

  resource: SitemapBundle/Resources/config/routing.yml

After that is done, you can access your sitemap at /sitemap.xml and siteindex at /siteindex.xml

Happy Coding

You can’t perform that action at this time.