https://github.com/tackk/cartographer
A PHP sitemap generation tool.
https://github.com/tackk/cartographer
Last synced: 27 days ago
JSON representation
A PHP sitemap generation tool.
- Host: GitHub
- URL: https://github.com/tackk/cartographer
- Owner: tackk
- License: mit
- Created: 2014-04-22T16:17:44.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-11-22T11:51:28.000Z (about 2 years ago)
- Last Synced: 2025-11-16T14:03:16.801Z (about 2 months ago)
- Language: PHP
- Homepage: http://tackk.github.io/cartographer/
- Size: 463 KB
- Stars: 327
- Watchers: 18
- Forks: 38
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-php-cn - 官网
- awesome-php - Cartographer - 一个站点地图生成库 (导航( Navigation ))
README
# Cartographer
[](https://packagist.org/packages/tackk/cartographer)
[](https://packagist.org/packages/tackk/cartographer)
[](https://packagist.org/packages/tackk/cartographer)
[](https://travis-ci.org/tackk/cartographer)
[](https://scrutinizer-ci.com/g/tackk/cartographer/)
[](https://scrutinizer-ci.com/g/tackk/cartographer/)
A sitemap generation tool for PHP following the [Sitemap Protocol v0.9](http://www.sitemaps.org/protocol.html).
Cartographer can handle Sitemaps of any size. When generating sitemaps with more than 50,000
entries (the limit), the sitemap becomes a "map of maps" (i.e. nested sitemaps).
* **GitHub Repo:** [http://github.com/tackk/cartographer/](http://github.com/tackk/cartographer/)
* **Documentation:** [http://tackk.github.io/cartographer/](http://tackk.github.io/cartographer/)
## Supported PHP/HHVM Versions
* **PHP:** >= 5.4 (including 5.6 beta1)
* **HHVM:** >= 3.0.0
## Installation
### Composer CLI
```
composer require tackk/cartographer:1.0.*
```
### composer.json
``` json
{
"require": {
"tackk/cartographer": "1.0.*"
}
}
```
## Basic Sitemap
If you have a sitemap that is under 50,000 items, you can just use the Sitemap class, and avoid the Sitemap
Generator.
``` php
use Tackk\Cartographer\Sitemap;
use Tackk\Cartographer\ChangeFrequency;
$sitemap = new Tackk\Cartographer\Sitemap();
$sitemap->add('http://foo.com', '2005-01-02', ChangeFrequency::WEEKLY, 1.0);
$sitemap->add('http://foo.com/about', '2005-01-01');
// Write it to a file
file_put_contents('sitemap.xml', (string) $sitemap);
// or simply echo it:
header ('Content-Type:text/xml');
echo $sitemap->toString();
```
### Output
``` xml
http://foo.com
2005-01-02T00:00:00+00:00
weekly
1
http://foo.com/about
2005-01-01T00:00:00+00:00
```
## Basic Sitemap Index
If you want to build a Sitemap Index, separate from the Sitemap Generator, you can!
``` php
$sitemapIndex = new Tackk\Cartographer\SitemapIndex();
$sitemapIndex->add('http://foo.com/sitemaps/sitemap.1.xml', '2012-01-02');
$sitemapIndex->add('http://foo.com/sitemaps/sitemap.2.xml', '2012-01-02');
// Write it to a file
file_put_contents('sitemap.xml', (string) $sitemapIndex);
// or simply echo it:
header ('Content-Type:text/xml');
echo $sitemapIndex->toString();
```
### Output
``` xml
http://foo.com/sitemaps/sitemap.1.xml
2012-01-02T00:00:00+00:00
http://foo.com/sitemaps/sitemap.2.xml
2012-01-02T00:00:00+00:00
```
## Sitemap Factory
The Sitemap Factory create Sitemaps and Sitemap Indexes and writes them to the Filesystem.
Is is can be used to generate full Sitemaps with more than **50,000** URLs.
If more than one sitemap is generated, it will create a Sitemap Index automatically.
### Instantiating
The factory uses [Flysystem](http://flysystem.thephpleague.com/) to write the sitemaps. This
means you can write the sitemaps to Local Disk, S3, Dropbox, wherever you want.
``` php
setBaseUrl('http://foo.com/sitemaps/');
```
You can get the current base URL using `getBaseUrl()`.
### Creating a Sitemap
To create a sitemap you use the `createSitemap` method. This method requires an `Iterator` as
its only parameter.
``` php
createSitemap($urls);
```
### Return Value
The two creation methods (`createSitemap` and `createSitemapIndex`) will return the URL
of the root sitemap file. If there is only 1 sitemap created, it will return just that URL.
If multiple sitemaps are created, then a Sitemap Index is generated and the URL to that is returned.
### List of Created Files
You can get a list (array) of files the Factory has created by using the `getFilesCreated` method.
``` php
$files = $sitemapFactory->getFilesCreated();
```
## Running Tests
*This assumes you have ran `composer update`.*
From the repository root, run:
```
vendor/bin/phpunit
```