Image

Sitemaper

A very flexible PHP sitemap generator library to suit all your sitemap needs

Sitemap PHP Sitemap Generator

Generating Sitemap Index Files

A sitemap index is a special sitemap that contains link to other sitemaps. Sitemap index can be used to split your sitemaps into chunk, or you can use it to serve sitemaps from other domains.

Sitemaper support writing index files, together with the linked sitemaps.

This is how you would create sitemap index files using the specified class method

Via class method

use Alexecus\Sitemaper\Sitemap;
use Alexecus\Sitemaper\SitemapIndex;

// Create sitemap and add items to it

$sitemapOne = new Sitemap('http://one.mysite.com', [
    ...
]);

// Create another sitemap and add items to it

$sitemapTwo = new Sitemap('http://two.mysite.com', [
    ...
]);

// specify the base domain for the sitemap index. We used a different domain
// because we are using mysite.com to serve the sitemap for two other sites.
//
// If you just want to split your sitemap into smaller chunks, keep the domains same
// across all sitemaps, including this index

$sitemapIndex = new SitemapIndex('http://mysite.com');

// The Add method accepts 2 arguments, the sitemap, and the filename
// the 2nd argument is just the filename, don't get confused as it is not a file path

$sitemapIndex->addSitemap($sitemapOne, 'sitemap-one.xml');
$sitemapIndex->addSitemap($sitemapTwo, 'sitemap-two.xml');

// Write the sitemap index, together with the sitemaps. Specify the directory
// wherein everything will be written

$sitemapIndex->write('public');

If you want, you can also create sitemap index files using the constructor approach

Via constructor

use Alexecus\Sitemaper\Sitemap;
use Alexecus\Sitemaper\SitemapIndex;

$sitemapIndex = new SitemapIndex('http://mysite.com', [
    'sitemap-one.xml' => new Sitemap('http://one.mysite.com', [
        ...
    ],
    'sitemap-two.xml' => new Sitemap('http://two.mysite.com', [
        ...
    ],
]);

$sitemapIndex->write('public');

You can change the filename of the generated index by specifying a third argument

use Alexecus\Sitemaper\Sitemap;
use Alexecus\Sitemaper\SitemapIndex;

$sitemapIndex = new SitemapIndex('http://mysite.com', [...], 'sitemap-index.xml');

$sitemapIndex->write('public');

Advanced Usage

You can even get the raw ouput of just the sitemap index if you wanted to

use Alexecus\Sitemaper\Sitemap;
use Alexecus\Sitemaper\SitemapIndex;

$sitemapIndex = new SitemapIndex('http://mysite.com', [
    'sitemap-one.xml' => new Sitemap('http://one.mysite.com', [
        ...
    ],
    'sitemap-two.xml' => new Sitemap('http://two.mysite.com', [
        ...
    ],
]);

$rawXml = $sitemapIndex->transform('xml');

// you can now do anything with the raw XML. Do note that it only contains the
// XML for the sitemap index file, not for the sitemap items