XML sitemap generation

5.0.0 2017-10-14 16:21 UTC

README

Notice: This repository is a mirror of what's hosted on GitLab (thepixeldeveloper/sitemap).

Thepixeldeveloper\Sitemap

pipeline status License Latest Stable Version Total Downloads Scrutinizer Code Quality

A tool to generate XML sitemaps.

Installation

composer require "thepixeldeveloper/sitemap"

Basic Usage

Generating a typical () sitemap.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Urlset;
use Thepixeldeveloper\Sitemap\Url;
use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

$url = new Url($loc);
$url->setLastMod($lastMod);
$url->setChangeFreq($changeFreq);
$url->setPriority($priority);

$urlset = new Urlset();
$urlSet->add($url);

$driver = new XmlWriterDriver();
$urlset->accept($driver);

echo $driver->getOutput();

Generating a parent () sitemap.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\SitemapIndex;
use Thepixeldeveloper\Sitemap\Sitemap;
use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

// Sitemap entry.
$url = new Sitemap($loc);
$url->setLastMod($lastMod);

// Add it to a collection.
$urlset = new SitemapIndex();
$urlSet->add($url);

$driver = new XmlWriterDriver();
$urlset->accept($driver);

echo $driver->getOutput();

Extensions

The following extensions are supported: Image, Link, Mobile, News and Video. They work in the following way (taking image as an example):

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Urlset;
use Thepixeldeveloper\Sitemap\Url;
use Thepixeldeveloper\Sitemap\Extensions\Image;

$url = new Url($loc);
$url->setLastMod($lastMod);
$url->setChangeFreq($changeFreq);
$url->setPriority($priority);

$image = new Image('https://image-location.com');

$url->addExtension($image);

...

Advanced Usage

Processing Instructions

You can add processing instructions on the output as such.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

$driver = new XmlWriterDriver();
$driver->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="/path/to/xslt/main-sitemap.xsl"');

Which will add before the document starts.

<?xml-stylesheet type="text/xsl" href="/path/to/xslt/main-sitemap.xsl"?>