Skip to content

heliopsis/DpnXmlSitemapBundle

 
 

Repository files navigation

dreipunktnull XML sitemap bundle

This bundle generates XML sitemaps for your favourite search engine by extracting sitemap information out of the site's routes.

Installation

The DpnXmlSitemapBundle files should be downloaded to the 'vendor/bundles/Dpn/XmlSitemapBundle' directory.

You can accomplish this in several ways, depending on your personal preference. The first method is the standard Symfony2 method.

Using composer

Add DpnXmlSitemapBundle in your composer.json:

{
    "require": {
        "dpn/xml-sitemap-bundle": "*"
    }
}

Using the vendors script

Add the following lines to your deps file:

[DpnXmlSitemapBundle]
    git=git://github.com/dreipunktnull/XmlSitemapBundle.git
    target=/bundles/Dpn/XmlSitemapBundle

Now, run the vendors script to download the bundle:

$ php bin/vendors install

Using submodules

If you prefer instead to use git submodules, then run the following:

$ git submodule add git://github.com/dreipunktnull/DpnXmlSitemapBundle.git vendor/bundles/Dpn/XmlSitemapBundle
$ git submodule update --init

Configure the Autoloader (if not using composer)

Now you will need to add the Dpn namespace to your autoloader:

<?php
// app/autoload.php

$loader->registerNamspaces(array(
    // ...
    'Dpn' => __DIR__.'/../vendor/bundles',
));

Enable the bundle

Finally, enable the bundle in the kernel:

<?php
// app/appKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Dpn\XmlSitemapBundle\DpnXmlSitemapBundle(),
    );
}

Register the routing in app/config/routing.yml

DpnXmlSitemapBundle:
    resource: "@DpnXmlSitemapBundle/Resources/config/routing.xml"

Configuration

Add the following configuration to your app/config/config.yml:

dpn_xml_sitemap: ~

The bundle uses a default priority of 0.5 and a changefreq of 'weekly'. You can override these defaults in your app/config/config.yml:

dpn_xml_sitemap:
    defaults:
        priority: 0.5
        changefreq: weekly

Usage

To expose a route to the sitemap add the option sitemap to your route definition:

<route id="my_route_to_expose" pattern="/find/me/in/sitemap">
    <default key="_controller">MyBundle:Controller:action</default>
    <option key="sitemap">
        <priority>0.7</priority>
        <changefreq>always</changefreq>
    </option>
</route>

or annotation

    /**
     * @Route("/find/me/in/sitemap", options={"sitemap" = {"priority" = 0.7, "changefreq" => "weekly", "check_format" => "true"}})
     */

or if you simply want to use the defaults:

    /**
     * @Route("/find/me/in/sitemap", options={"sitemap" = true})
     */

The generated sitemap is then available under the url /sitemap.xml and the bundle will throw a not found exception if it doesn't contain any routes.

Sitemap Index

According to sitemaps.org the maximum number of entries a sitemap.xml may have is 50,000. When the number of sitemap entries exceeds this, the entires are split across multiple sitemaps (ie /sitemap1.xml,/sitemap2.xml.../sitemapN.xml).

A sitemap index is accessible at /sitemap_index.xml.

The maximum entries per sitemap is configurable:

dpn_xml_sitemap:
    max_per_sitemap: 50000 #default

HTTP Caching

You can enable default http caching for the sitemap.xml route by setting the number of seconds in your config:

dpn_xml_sitemap:
    http_cache: 3600

Check Format

By default, there is a simple formatting check that will help prevent incorrect routes. However, if you have a site that uses trailing / and you're sure the routes are correct, you may want to disable the check in the configuration and when you call the setUri method.

Full Default Configuration

dpn_xml_sitemap:
    http_cache:               ~
    max_per_sitemap:          50000
    check_format:             true
    defaults:
        priority:             0.5
        changefreq:           weekly

Custom Generator

By default, the bundle looks at your routing config to find routes that you have the sitemap option set. You can also create your own sitemap entry generator:

  1. Create a generator class that implements the Dpn\XmlSitemapBundle\Sitemap\GeneratorInterface interface. This class must have a generate() method that returns an array of Dpn\XmlSitemapBundle\Sitemap\Entry objects. Here is an example:

    class MySitemapGenerator implements \Dpn\XmlSitemapBundle\Sitemap\GeneratorInterface
    {
        public function generate()
        {
            $entries = array();
    
            $entry = new \Dpn\XmlSitemapBundle\Sitemap\Entry();
            $entry->setUri('/foo');
            $entries[] = $entry;
    
            // add more entries - perhaps fetched from database
    
            return $entries;
        }
    }
  2. Add this class as a service tagged with dpn_xml_sitemap.generator:

    services:
        my.sitemap_generator:
            class: MySitemapGenerator
            tags:
                - { name: dpn_xml_sitemap.generator }

License

See Resources/meta/LICENSE.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%