This bundle generates XML sitemaps for your favourite search engine by extracting sitemap information out of the site's routes.
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.
Add DpnXmlSitemapBundle in your composer.json:
{
"require": {
"dpn/xml-sitemap-bundle": "*"
}
}
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
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
Now you will need to add the Dpn
namespace to your autoloader:
<?php
// app/autoload.php
$loader->registerNamspaces(array(
// ...
'Dpn' => __DIR__.'/../vendor/bundles',
));
Finally, enable the bundle in the kernel:
<?php
// app/appKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Dpn\XmlSitemapBundle\DpnXmlSitemapBundle(),
);
}
DpnXmlSitemapBundle:
resource: "@DpnXmlSitemapBundle/Resources/config/routing.xml"
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
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.
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
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
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.
dpn_xml_sitemap:
http_cache: ~
max_per_sitemap: 50000
check_format: true
defaults:
priority: 0.5
changefreq: weekly
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:
-
Create a generator class that implements the
Dpn\XmlSitemapBundle\Sitemap\GeneratorInterface
interface. This class must have agenerate()
method that returns an array ofDpn\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; } }
-
Add this class as a service tagged with
dpn_xml_sitemap.generator
:services: my.sitemap_generator: class: MySitemapGenerator tags: - { name: dpn_xml_sitemap.generator }
See Resources/meta/LICENSE
.