Developed with love by KnpLabs Hire us for your project!
47

DpnXmlSitemapBundle

by bjo3rnf

DpnXmlSitemapBundle

Build Status
Scrutinizer Code Quality
Code Coverage
Latest Stable Version
License

This bundle generates XML sitemaps for your favourite search engine by extracting
sitemap information out of your application's routes. Additionally, you can create
your own generators to provide URLs. The sitemap(s) generated follow the
sitemap protocol.

Installation

  1. Install with composer:

    composer require dpn/xml-sitemap-bundle
    
  2. Enable the bundle in the kernel:

    // app/AppKernel.php
    
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Dpn\XmlSitemapBundle\DpnXmlSitemapBundle(),
        );
    }
    
  3. Register the routing in app/config/routing.yml (this step is optional if using the console
    command to pre-generate the sitemaps)

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

Usage

Exposing Routes

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

blog_index:
    path:      /blog
    defaults:  { _controller: AppBundle:Blog:index }
    options:
        sitemap: true

This will expose this route to your sitemap using the default options from your config. To control the options
for this sitemap entry, add them to the sitemap option:

blog_index:
    path:      /blog
    defaults:  { _controller: AppBundle:Blog:index }
    options:
        sitemap:
                priority: 0.7
                changefreq: hourly

NOTE: Only routes without parameters may be exposed in this way. For routes with parameters, you must create
a custom generator (see below).

Custom Generator

For more complex routes that have parameters, you must create a custom generator.

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

    use Dpn\XmlSitemapBundle\Sitemap\Entry;
    use Dpn\XmlSitemapBundle\Sitemap\GeneratorInterface;
    
    class MySitemapGenerator implements GeneratorInterface
    {
        public function generate()
        {
            $entries = array();
    
            $entries[] = new Entry('http://example.com/foobar'); // must be absolute URL
    
            // 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 }
    

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 entries 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 http caching for the sitemap(n).xml/sitemap_index.xml URI's by setting the number of
seconds in your config:

dpn_xml_sitemap:
    http_cache: 3600

Console Dump Command

The dpn:xml-sitemap:dump command is available to pre-generate sitemap.xml files:

Usage:
 dpn:xml-sitemap:dump [--target="..."] host

Arguments:
 host      The full hostname for your website (ie http://www.google.com)

Options:
 --target  Override the target directory to dump sitemap(s) in

Help:
 Dumps your sitemap(s) to the filesystem (defaults to web/)

NOTE: The command requires Symfony 2.4+.

Full Default Configuration

The following is the default configuration for this bundle:

dpn_xml_sitemap:

    # The length of time (in seconds) to cache the sitemap/sitemap_index xml\'s (a reverse proxy is required)
    http_cache:      ~

    # The number of url entries in a single sitemap
    max_per_sitemap: 50000

    # The default options for sitemap URL entries to be used if not overridden
    defaults:

        # Value between 0.0 and 1.0 or null for sitemap protocol default
        priority:   ~

        # One of [always, hourly, daily, weekly, monthly, yearly, never] or null for sitemap protocol default
        changefreq: ~

License

See Resources/meta/LICENSE.

Copyright (c) <2014> Björn Fromme

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
dpn_xml_sitemap:

# The length of time (in seconds) to cache the sitemap/sitemap_index xml's (a reverse proxy is required)
http_cache: ~

# The number of url entries in a single sitemap
max_per_sitemap: 50000

# The default options for sitemap URL entries to be used if not overridden
defaults:

# Value between 0.0 and 1.0 or null for sitemap protocol default
priority: ~

# One of [always, hourly, daily, weekly, monthly, yearly, never] or null for sitemap protocol default
changefreq: ~
  • Merge pull request #36 from kbond/dep-fix
    By web-flow, 3 years ago
  • drop symfony 2.3 support (closes #35)
    By kbond, 3 years ago
  • Merge pull request #34 from kbond/symfony-stable
    By kbond, 4 years ago
  • fix for absolute url
    By kbond, 4 years ago
  • symfony 2.8/3.0 stable
    By kbond, 4 years ago
  • Merge pull request #33 from kbond/travis
    By kbond, 4 years ago
  • update .scrutinizer.yml filter
    By kbond, 4 years ago
  • ensure proper version of symfony/dependency-injection
    By kbond, 4 years ago
  • add to travis build matrix
    By kbond, 4 years ago
  • add .gitattributes/.scrutinizer.yml, rename phpunit.xml.dist
    By kbond, 4 years ago
  • Merge pull request #32 from bitundpixel/master
    By kbond, 4 years ago
  • Update README.md
    By bitundpixel, 4 years ago
  • Merge pull request #31 from kbond/travis
    By kbond, 4 years ago
  • allow symfony 3.0, update travis build matrix
    By kbond, 4 years ago
  • fix deprecated routing config, added symfony 2.7 to travis
    By kbond, 4 years ago
  • test on Symfony 2.6
    By kbond, 5 years ago
  • Merge pull request #28 from kbond/updates
    By bjo3rnf, 5 years ago
  • added command docs
    By kbond, 5 years ago
  • update readme
    By kbond, 5 years ago
  • fixes per scrutinizer
    By kbond, 5 years ago
  • send code coverage to scrutinizer
    By kbond, 5 years ago
  • refactored dump command
    By kbond, 5 years ago
  • fixed timing issue in test, added functional test for non http_cached sitemaps
    By kbond, 5 years ago
  • added missing license header
    By kbond, 5 years ago
  • change file permisions
    By kbond, 5 years ago
  • added controller tests
    By kbond, 5 years ago
  • make services private
    By kbond, 5 years ago
  • make symfony 2.3+ update .travis.yml
    By kbond, 5 years ago
  • change to psr-4
    By kbond, 5 years ago
  • added tests
    By kbond, 5 years ago