PHP 网站地图生成工具。

1.1.0 2015-05-01 18:41 UTC

This package is not auto-updated.

Last update: 2024-09-10 02:19:49 UTC


README

Latest Stable Version Total Downloads License

Build Status Code Coverage Scrutinizer Code Quality

遵循 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 Index

如果您想构建一个与 Sitemap 生成器分开的 Sitemap Index,您也可以做到!

$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 Index,并将它们写入文件系统。它可以用来生成超过 50,000 个 URL 的完整 Sitemap。

如果生成了多个 Sitemap,它将自动创建一个 Sitemap Index。

实例化

工厂使用 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 Index 和返回的入口点 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);

返回值

两个创建方法(createSitemapcreateSitemapIndex)将返回根 Sitemap 文件的 URL。如果只创建了一个 Sitemap,它将返回该 URL。如果有多个 Sitemap 被创建,则生成一个 Sitemap Index,并返回该 URL。

已创建文件列表

您可以通过使用 getFilesCreated 方法获取工厂已创建的文件列表(数组)。

$files = $sitemapFactory->getFilesCreated();

运行测试

这假设您已经运行了 composer update

从仓库根目录运行

vendor/bin/phpunit