Skip to content
PHP XML Sitemap Generation
PHP
Branch: master
Clone or download
Latest commit 516bea7 Mar 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE
.idea
.semaphore Import CodeCov secrets. Mar 6, 2019
src Fix linting errors. Mar 6, 2019
tests
.gitattributes Ignore phpunit files in a git export. Nov 19, 2017
.gitignore
CHANGELOG.md Update changelog. Nov 19, 2017
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md (#60) Feb 16, 2018
CONTRIBUTING.md
LICENSE Dependency updates. (#51) Sep 18, 2017
PULL_REQUEST_TEMPLATE.md
README.md Fix README typo Mar 13, 2019
composer.json Use codecov Mar 6, 2019
phpstan.neon Fix linting errors. Mar 6, 2019
phpunit.xml Write to coverage.xml Mar 6, 2019

README.md

Thepixeldeveloper\Sitemap

codecov License Latest Stable Version Total Downloads

A tool to generate XML sitemaps. Integrates with Symfony via SitemapBundle

Installation

composer require "thepixeldeveloper/sitemap"

Basic Usage

Generating a typical (<urlset>) 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->output();

Generating a parent (<sitemapindex>) 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->output();

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->addProcessingInstructions('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"?>

Comments

Comments are useful for information such as when the file was created.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

$date = date('Y-m-d H:i:s');

$driver = new XmlWriterDriver();
$driver->addComment('This XML file was written on ' . $date . '. Bye!');

Which will render out.

<?xml version="1.0" encoding="UTF-8"?>
<!--This XML file was written on 2018-06-24 15:57:23. Bye!-->
You can’t perform that action at this time.