PHPackages                             lukaswhite/sitemap - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lukaswhite/sitemap

ActiveProject[Utility &amp; Helpers](/categories/utility)

lukaswhite/sitemap
==================

A PHP library for managing and generating sitemaps

1.0(7y ago)0188MITPHPPHP &gt;=7.1.0

Since Nov 12Pushed 7y ago1 watchersCompare

[ Source](https://github.com/lukaswhite/sitemap)[ Packagist](https://packagist.org/packages/lukaswhite/sitemap)[ RSS](/packages/lukaswhite-sitemap/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Sitemap
=======

[](#sitemap)

A PHP 7.1+ library for building sitemaps, to be written to various formats; most notably as an XML sitemap.

[![CircleCI](https://camo.githubusercontent.com/a29088855e9ab312dbdf726aaba4c670602be54284a0d4ef4412226eec3d9111/68747470733a2f2f636972636c6563692e636f6d2f67682f6c756b617377686974652f736974656d61702e7376673f7374796c653d737667)](https://circleci.com/gh/lukaswhite/sitemap)

Installation
------------

[](#installation)

Install the package using Composer:

```
composer require lukaswhite/sitemaps

```

> This package requires PHP version 7.1 or greater

Creating a Sitemap
------------------

[](#creating-a-sitemap)

```
use Lukaswhite\Sitemap\Sitemap;
$sitemap = Sitemap( );
```

You can also provide the location of the sitemap. This is required for certain formats, such as RSS and Atom.

```
$sitemap->setLocation( 'http://example.com/sitemap.rss' );
```

It's also a good idea to set the date &amp; time that the sitemap was modified; particularly if you're generating a sitemap list:

```
$sitemap->setLastModified( new \DateTime( ) );
// or
$sitemap->setLastModified(
	( new \DateTime( ) )->setDate( 2018, 11, 15 )->setTime( 9, 45, 0 )
);
// or, if youre using Carbon:
$sitemap->setLastModified( Carbon::now( ) );
```

Adding Pages
------------

[](#adding-pages)

To create a page with the minimum information:

```
use Lukaswhite\Sitemap\Page;

$sitemap
	->addPage(
		new Page(
			'http://www.example.com'
		)
	);
```

You may also provide a title, the date &amp; time the page was last modified, the sitemap priority and/or the change frequency:

```
use Lukaswhite\Sitemap\Page;

$sitemap
	->addPage(
		new Page(
			'http://www.example.com',
			'Homepage',
			( new \DateTime( ) )->setDate( 2018, 11, 20 ),
			1,
			Page::CHANGE_FREQ_MONTHLY
		)
);
```

> The title isn't used for XML sitemaps, but it's required for RSS and Atom sitemaps.

> The sitemap priority is a float between 0 and 1, and if it isn't provided then it defaults to 0.5.

There are also some convenience methods for setting the page frequency:

```
$page->alwaysChanges( );
$page->changesYearly( );
$page->changesMonthly( );
$page->changesWeekly( );
$page->changesDaily( );
$page->changesHourly( );
$page->neverChanges( );
```

Creating an XML Sitemap
-----------------------

[](#creating-an-xml-sitemap)

Once you've created your sitemap programmatically, the most common scenario is probably to render it as an XML sitemap.

```
use Lukaswhite\Sitemap\Writer\Xml;

$writer = new Xml( $sitemap );

$xml = $writer->write( );
// or
$writer->save( '/path/to/sitemap.xml' );
```

If you want to provide an XSL stylesheet, which means that you can provide it in HTML format if someone visits it in a browser directly, you can do so like this:

```
$writer->addXslStylesheet( 'http://example.com/styles.xsl' );
```

You can add any additional processing instructions using `addProcessingInstruction( $name, value )`.

You may also wish to add comments; for example, you could use this to display aa human-readable indication of when the sitemap was generated, or how it was generated.

```
$writer->addComment( sprintf( 'Generated on %s.', date('d/m/Y' ) );
$writer->addComment( 'Generated using Sitemap by Lukas White' );
```

Plain-text Sitemaps
-------------------

[](#plain-text-sitemaps)

A plain-text sitemap is simply a text file, with the URLs that make up the sitemap separated by newlines. They're supported by Google as an alternative to XML sitemaps, although note that information such as the priority and change frequency isn't included by design, so they're rather more limited than XML sitemaps.

```
use Lukaswhite\Sitemap\Writer\Text;

$writer = new Text( $sitemap );

$text = $writer->write( );
// or
$writer->save( '/path/to/sitemap.txt' );
```

RSS Sitemaps
------------

[](#rss-sitemaps)

You can also create a sitemap in RSS format.

> The RSS writer is pretty limited; You may wish to consider using [my feed writer package](https://github.com/lukaswhite/php-feed-writer) instead.

```
use Lukaswhite\Sitemap\Writer\RSS;

$writer = new RSS( $sitemap );

$feed = $writer->write( );
// or
$writer->save( '/path/to/sitemap.rss' );
```

> You must set the location of the sitemap in order to create a valid feed.

Note that a valid RSS feed requires that the `` element includes a title and description, although they aren't really required for sitemaps. Because they are required for the feed to validate, the library outputs some defaults. If you wish, though, you can override these:

```
$writer->setChannelTitle( 'My Sitemap' );
$writer->setChannelDescription( 'This is a sitemap' );
```

Atom Sitemaps
-------------

[](#atom-sitemaps)

You can also create a sitemap in Atom format.

> The Atom writer is pretty limited; You may wish to consider using [my feed writer package](https://github.com/lukaswhite/php-feed-writer) instead.

```
use Lukaswhite\Sitemap\Writer\Atom;

$writer = new Atom( $sitemap );

$feed = $writer->write( );
// or
$writer->save( '/path/to/sitemap.atom' );
```

> You must set the location of the sitemap in order to create a valid feed.

Note that a valid Atom feed requires that the `` element includes a title, ID and author, although they aren't really required for sitemaps. Because they are required for the feed to validate, the library outputs some defaults. If you wish, though, you can override these:

```
$writer->setFeedId( 'my-custom-feed-id' );
$writer->setFeedTitle( 'My Atom Sitemap Feed' );
$writer->setFeedAuthor( 'Bob', 'bob@example.com' );
```

You can also set the ID on individual pages using -`>setId()`.

HTML Sitemaps
-------------

[](#html-sitemaps)

You can use the HTML writer to generate an HTML-based sitemap.

It's pretty simple; it simply generates an unordered list of links.

```
use Lukaswhite\Sitemap\Writer\Html;

$writer = new Html( $sitemap );

$ul = $writer->write( );
```

Here's an example:

```
$sitemap = new Sitemap( );

$sitemap
	->addPage( new Page(
	    'http://www.example.com',
	    'Homepage'
	) )
	->addPage( new Page(
		'http://www.example.com/about'
	)
);
```

The result:

```

	Homepage
	http://www.example.com/about

```

JSON Sitemaps
-------------

[](#json-sitemaps)

JSON isn't a format generally used for sitemaps. However, there are some circumstances in which it might be useful.

For a large application, it might be helpful to be able to inspect a sitemap from an admin panel. One way of doing this is to build a component that displays the current sitemap from a JSON API; that's where this writer might come in.

```
use Lukaswhite\Sitemap\Writer\Json;

$writer = new Json( $sitemap );

$feed = $writer->write( );
// or
$writer->save( '/path/to/sitemap.json' );
```

Custom Writers
--------------

[](#custom-writers)

You're free to create your own custom writer; simply extend `AbstractWriter` and implement the `write()` method.

Getting Data
------------

[](#getting-data)

Alternatively, you may wish to pull data out of the sitemap for manipulation, sorting, or other purposes.

The `getPages()` method on the `Sitemap` class returns an array of instances of the `Page` class. This in turn provides methods such as `getUrl()`, `getTitle()`, `getPriority()` and `getChangeFreq()`.

Splitting Up Large Sitemaps
---------------------------

[](#splitting-up-large-sitemaps)

If your sitemap starts to get very large, you may split it up into separate files. This is done by creating a **sitemap list**.

```
use Lukaswhite\Sitemap\SitemapList;
use Lukaswhite\Sitemap\Sitemap;

$list = new SitemapList( );
$list
	->addSitemap(
		( new Sitemap( ) )->setLocation(
			'http://example.com/sitemap1.xml'
		)->setLastModified( ( new \DateTime( ) )->setDate( 2018, 11, 20 ) )
	)
	->addSitemap(
		( new Sitemap( ) )->setLocation(
			'http://example.com/sitemap2.xml'
		)->setLastModified( ( new \DateTime( ) )->setDate( 2018, 11, 24 ) )
);

$list = $writer->write( );
// or
$list->save( '/path/to/sitemap-list.xml' );
```

> Note that in order to create a sitemap, you MUST set the location of the sitemap, for obvious reasons.

> You need to create a Sitemap instance in order to craete a sitemap list, but you don't necessarily need to build them fully at the same time, since all the sitemap list includes is the URL and last modified date/time.

Sorting the Sitemap
-------------------

[](#sorting-the-sitemap)

Most often, the library will probably be used for generating XML sitemaps; in which case the order of the entries makes no difference.

However if you're creating a JSON representation for inspecting in your back-end, or an HTML representation to embed in a page then it might be useful to sort the sitemap; for example by priority.

```
$sitemap->sortByPriority( );
// or
$sitemap->sortByTitle( );
```

Extensions
----------

[](#extensions)

The library supports the following extensions:

- Links
- Images
- Videos

### Links

[](#links)

The links extension allows you to specify alternate version of pages, either in different languages or for specified locales. For example in Spanish, or in English for British audiences.

You still need to set the URL of a page as normal — this is then the "default" location of the page — and then you can add links as follows:

```
$page->addAlternate( 'http://www.example.com/de', 'de' );
$page->addAlternate( 'http://www.example.com/gb', 'en-GB' );
```

### Images

[](#images)

You can associate one or more images with a page for more effective search engine crawling. an image requires a location (URL), but may also have a title, caption, geo-location and / or a license.

Here's a minimal example:

```
use Lukaswhite\Sitemap\Extensions\Image;

$page->addImage(
	new Image( 'http://www.example.com/image1.jpg' )
);
```

Here's a more comprehensive example:

```
use Lukaswhite\Sitemap\Extensions\Image;

$page->addImage(
	( new Image( 'http://www.example.com/image1.jpg' ) )
	->setCaption( 'Test image caption' )
	->setTitle( 'Test image title' )
	->setGeoLocation( 'London' )
	->setLicense( 'http://www.example.com/license.html' )
);
```

### Videos

[](#videos)

You may also associate one or more videos with a page.

Here is an example that shows most of the available options:

```
use Lukaswhite\Sitemap\Extensions\Video;
use Lukaswhite\Sitemap\Support\Video\Player;
use Lukaswhite\Sitemap\Support\Video\Price;
use Lukaswhite\Sitemap\Support\Video\Platform;
use Lukaswhite\Sitemap\Support\Video\Resriction;
use Lukaswhite\Sitemap\Support\Video\Uploader;

$page->addVideo(
$video =
	( new Video(  ) )
	->setTitle( 'Grilling steaks for summer' )
	->setDescription( 'Alkis shows you how to get perfectly done steaks every time' )
	->setContentLocation( 'http://streamserver.example.com/video123.mp4' )
	->setPlayer(
		new Player(
			'http://www.example.com/videoplayer.php?video=123',
			true // allow embedding
		)
	)
	->setDuration(600 )
	->setExpirationDate(
		( new \DateTime( ) )->setDate( 2021, 11, 05 )
		->setTime( 19, 20, 30 )
	)
	->setPublicationDate(
		( new \DateTime( ) )->setDate( 2007, 11, 05 )
		->setTime( 19, 20, 30 )
	)
	->setRating( 4.2 )
	->setPrice(
		( new Price( 1.99, 'EUR' ) )
		->setType( Price::OWN )
		->setResolution( Price::HD )
	)
	->addRestriction(
		( new Restriction( 'IE', 'GB', 'US', 'CA' ) )
		->allows( )
		)
	->setPlatform(
		( new Platform(
			Platform::TV,
			Platform::WEB
		) )
		->denies( )
	)
	->setLive( )
	->setFamilyFriendly( )
	->setRequiresSubscription( )
	->setViewCount( 1000 )
	->setTags( 'steak', 'grilling', 'summer' )
	->setCategories( 'Cooking', 'Beef' )
	->setUploader(
		new Uploader(
			'GrillyMcGrillerson',
			'http://www.example.com/users/grillymcgrillerson' // optional
		)
	);
);
```

Some notes on the example above:

- In the example the content location (i.e. the URL of the media file) is specified; either this *or* a URL to a thumbnail is required — you can set that using `setThumbnailLocation()`
- The price in the example is to own the high definition version; you can set the type to "rent" and/or the resolution to "sd", though the only required information is the actual price and the currency
- You can specify per-country restrictions; in this example we're allowing people in Great Britain, Ireland, the US and Canada to view it. You can also create a restriction type that denies access to certain countries
- You can allow or deny access to particular platforms (web, mobile, TV). In this example we're denying access to TV and Web; therefore making it mobile-only

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2740d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1bd633ffdf0f66a73a288c4edcacfa43d214cd95a53384c6fe5ea4b3554dd9d?d=identicon)[lukaswhite](/maintainers/lukaswhite)

---

Top Contributors

[![lukaswhite](https://avatars.githubusercontent.com/u/999014?v=4)](https://github.com/lukaswhite "lukaswhite (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lukaswhite-sitemap/health.svg)

```
[![Health](https://phpackages.com/badges/lukaswhite-sitemap/health.svg)](https://phpackages.com/packages/lukaswhite-sitemap)
```

###  Alternatives

[beebmx/kirby-env

Enable env variables to Kirby

2037.9k2](/packages/beebmx-kirby-env)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
