Quick links: Content - sections - sub sections
EN FR

jResponseSitemap allow you to generate a sitemap, used by search engines like Google.

Download and install

The class jResponseSitemap and its dependencies are in a separate module, that you have to download and install. A zip file is available, or you can install it with Composer: package jelix/sitemap-module.


composer require "jelix/sitemap-module"

Then activate and declare the module into the configuration:


php dev.php module:configure jsitemap

Then launch the installer:


php install/installer.php

Usage

Retrieve the response object in the controller:


$resp = $this->getResponse('sitemap');

You have then a jResponseSitemap object in $resp.

Restrictions in a sitemap

  • A sitemap file cannot contain more than 50 000 URLs.
  • A sitemap index file cannot declare more than 1000 sitemap files.

Manual generation

Creation of a sitemap is done by adding each URLs you want to declare in the file:


$resp->addUrl($loc, $lastmod, $changefreq, $priority);
  • $loc: URL of the page
  • $lastmod: date of the last modification of the page. format: Y-m-d.
  • $changefreq: Frequency of the page modifications (only these value: always, hourly, daily, weekly, monthly, yearly, never)
  • $priority: priority of the url beside other URL you declare.

Only $loc is required.

Example:


$resp->addUrl(jUrl::get('module~home:index'));
$resp->addUrl(jUrl::get('module~contact:index'), null, 'weekly', 1);

Automatic generation from the urls.xml file

This behavior is partially possible, by using urls indicated into the app/system/urls.xml.

You just have to call one method:


$resp->importFromUrlsXml();
return $resp;

It doesn't import url that have dynamique pathinfo, or those that are common for an entire module.

Index generation

If you have a big web site, it is prefered to have many files, and declare them in an index. You declare by calling this method:


$resp->addSitemap($loc, $lastmod)
  • $loc: URL of a sitemap
  • $lastmod: date of the last modification of the page. format: Y-m-d.

Only $loc is required.

It means that you have to create as many action as you want to generate sitemap files, and declare their URL in the urls.xml file. In an other action, you generate the index. You can call jUrl::get for each sitemap and assign this value to $loc


$resp->addSitemap(jUrl::get('mymodule~sitemap:s1');
$resp->addSitemap(jUrl::get('mymodule~sitemap:s2');
$resp->addSitemap(jUrl::get('mymodule~sitemap:s3');
$resp->addSitemap(jUrl::get('mymodule~sitemap:s4');
return $resp;

(s1, s2, s3 and s4 are actions where you generate a sitemap).

Registering your sitemap into search engines

To give the address of your sitemap to search engines, you can do it manually by filling the corresponding form on the web site of the search engine.

You can also do it automatically, by giving the URL of the registration process of the search engine, to the ping() method:


$sitemapurl = jUrl::getFull('mymodule~sitemap:index');

// google
$resp->ping('http://www.google.com/webmasters/tools/ping?sitemap=' . $url);

// yahoo
$resp->ping('http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=YahooDemo&url=' . $url);