gsouf / cartographer
PHP 网站地图生成工具。
Requires
- php: >=5.6
- league/flysystem: 1.0.*
Requires (Dev)
- phpunit/phpunit: 4.0.*
README
注意:这是一个对原始工作的分支,增加了对 Google 图像扩展的支持。
遵循 Sitemap Protocol v0.9 的 PHP 网站地图生成工具。
Cartographer 可以处理任何大小的 Sitemap。当生成超过 50,000 条条目(限制)的 Sitemap 时,Sitemap 将成为“地图的地图”(即嵌套 Sitemap)。
支持的 PHP/HHVM 版本
- PHP: >= 5.4(包括 5.6 beta1)
- HHVM >= 3.0.0
安装
Composer CLI
composer require tackk/cartographer:1.0.*
composer.json
{ "require": { "tackk/cartographer": "1.0.*" } }
基本 Sitemap
如果您有一个小于 50,000 项的 Sitemap,您可以直接使用 Sitemap 类,并避免使用 Sitemap 生成器。
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();
输出
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://foo.com</loc> <lastmod>2005-01-02T00:00:00+00:00</lastmod> <changefreq>weekly</changefreq> <priority>1</priority> </url> <url> <loc>http://foo.com/about</loc> <lastmod>2005-01-01T00:00:00+00:00</lastmod> </url> </urlset>
基本 Sitemap 索引
如果您想构建一个独立的 Sitemap 索引,而不是使用 Sitemap 生成器,您也可以做到!
$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();
输出
<?xml version="1.0" encoding="UTF-8"?> <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://foo.com/sitemaps/sitemap.1.xml</loc> <lastmod>2012-01-02T00:00:00+00:00</lastmod> </url> <url> <loc>http://foo.com/sitemaps/sitemap.2.xml</loc> <lastmod>2012-01-02T00:00:00+00:00</lastmod> </url> </sitemapindex>
Sitemap 工厂
Sitemap 工厂创建 Sitemap 和 Sitemap 索引,并将它们写入文件系统。它可以用来生成超过 50,000 个 URL 的完整 Sitemap。
如果生成多个 Sitemap,它将自动创建一个 Sitemap 索引。
实例化
工厂使用 Flysystem 来写入 Sitemap。这意味着您可以将 Sitemap 写入本地磁盘、S3、Dropbox 等任何您想的地方。
<?php use League\Flysystem\Filesystem; use League\Flysystem\Adapter\Local as LocalAdapter; $adapter = new LocalAdapter(__DIR__.'/sitemaps'); $filesystem = new Filesystem($adapter); $sitemapFactory = new Tackk\Cartographer\SitemapFactory($filesystem);
基础 URL
基础 URL 用于生成 Sitemap 索引以及返回的入口点 URL。
您可以为基础 URL 设置值。
$sitemapFactory->setBaseUrl('http://foo.com/sitemaps/');
您可以使用 getBaseUrl()
获取当前的基础 URL。
创建 Sitemap
要创建 Sitemap,您使用 createSitemap
方法。此方法只需要一个 Iterator
作为参数。
<?php use League\Flysystem\Filesystem; use League\Flysystem\Adapter\Local as LocalAdapter; $adapter = new LocalAdapter(__DIR__.'/sitemaps'); $filesystem = new Filesystem($adapter); $sitemapFactory = new Tackk\Cartographer\SitemapFactory($filesystem); // Create an Iterator of your URLs somehow. $urls = get_url_iterator(); // Returns the URL to the main Sitemap/Index file $mainSitemap = $sitemapFactory->createSitemap($urls);
Google 图像扩展
使用简单的 Sitemap
<?php use Tackk\Cartographer\GoogleSitemap; use Tackk\Cartographer\ChangeFrequency; $sitemap = new Tackk\Cartographer\GoogleSitemap(); $sitemap->add('http://foo.com', '2005-01-02', ChangeFrequency::WEEKLY, 1.0); // Adds an image for the previous url $sitemap->addImage('http://foo.com/bar.jpg', 'image title', 'image caption', 'geo location', 'license'); // Write it to a file file_put_contents('sitemap.xml', (string) $sitemap);
使用 Sitemap 工厂,迭代器应返回一个数组,其中包含 URL,并包含一个包含该 URL 所有可能图片的 'images' 键。
<?php use League\Flysystem\Filesystem; use League\Flysystem\Adapter\Local as LocalAdapter; $adapter = new LocalAdapter(__DIR__.'/sitemaps'); $filesystem = new Filesystem($adapter); $sitemapFactory = new Tackk\Cartographer\GoogleSitemapFactory($filesystem); // Create an Iterator of your URLs somehow. $urls = get_url_iterator(); // Url iterator items format: // ['url' => 'http://foo.com', 'images' => [ // ['loc' => 'http://foo.com/bar.jpg'] // ] // Returns the URL to the main Sitemap/Index file $mainSitemap = $sitemapFactory->createSitemap($urls);
返回值
两个创建方法(createSitemap
和 createSitemapIndex
)将返回根 Sitemap 文件的 URL。如果只创建了一个 Sitemap,它将只返回该 URL。如果创建了多个 Sitemap,则生成一个 Sitemap 索引,并返回该 URL。
已创建文件列表
您可以通过使用 getFilesCreated
方法获取工厂已创建的文件列表(数组)。
$files = $sitemapFactory->getFilesCreated();
运行测试
这假设您已经运行了 composer update
。
从仓库根目录运行
vendor/bin/phpunit