A tool to generate XML sitemaps. Integrates with Symfony via SitemapBundle
composer require "thepixeldeveloper/sitemap"
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();
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);
...
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!-->
中文补充
增加百度蜘蛛专用协议
use Thepixeldeveloper\Sitemap\Urlset;
use Thepixeldeveloper\Sitemap\Url;
use Thepixeldeveloper\Sitemap\Extensions\Mobile;
use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;
$url = new Url($loc);
$url->setLastMod($lastMod);
$url->setChangeFreq($changeFreq);
$url->setPriority($priority);
$mobile = new Mobile();
$mobile->setType('pc,mobile');
$url->addExtension($mobile);
$urlset = new Urlset();
$urlset->add($url);
$driver = new XmlWriterDriver();
$urlset->accept($driver);
echo $driver->output();
输出示例
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://w
ww.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.
com/schemas/sitemap-mobile/1/">
<url>
<loc>http://baidu.com</loc>
<mobile:mobile type="pc,mobile"/>
</url>
</urlset>