fr05t1k/sitemap

网站地图和网站地图索引生成器

2.0.3 2016-04-28 23:16 UTC

This package is auto-updated.

Last update: 2024-09-10 00:05:36 UTC


README

网站地图和网站地图索引生成器。

特性

  • 创建网站地图文件。
  • 创建网站地图索引文件。
  • 当达到50000个URL限制时,自动创建新文件。
  • 可配置大小的内存高效缓冲区。

安装

通过Composer安装非常简单

composer require samdark/sitemap

之后,确保您的应用程序通过包含 vendor/autoload.php 来自动加载Composer类。

如何使用它

use samdark\sitemap\Sitemap;
use samdark\sitemap\Index;

// create sitemap
$sitemap = new Sitemap(__DIR__ . '/sitemap.xml');

// add some URLs
$sitemap->addItem('http://example.com/mylink1');
$sitemap->addItem('http://example.com/mylink2', time());
$sitemap->addItem('http://example.com/mylink3', time(), Sitemap::HOURLY);
$sitemap->addItem('http://example.com/mylink4', time(), Sitemap::DAILY, 0.3);

// write it
$sitemap->write();

// get URLs of sitemaps written
$sitemapFileUrls = $sitemap->getSitemapUrls('http://example.com/');

// create sitemap for static files
$staticSitemap = new Sitemap(__DIR__ . '/sitemap_static.xml');

// add some URLs
$staticSitemap->addItem('http://example.com/about');
$staticSitemap->addItem('http://example.com/tos');
$staticSitemap->addItem('http://example.com/jobs');

// write it
$staticSitemap->write();

// get URLs of sitemaps written
$staticSitemapUrls = $staticSitemap->getSitemapUrls('http://example.com/');

// create sitemap index file
$index = new Index(__DIR__ . '/sitemap_index.xml');

// add URLs
foreach ($sitemapFileUrls as $sitemapUrl) {
    $index->addSitemap($sitemapUrl);
}

// add more URLs
foreach ($staticSitemapUrls as $sitemapUrl) {
    $index->addSitemap($sitemapUrl);
}

// write it
$index->write();

选项

配置 Sitemap 实例有两种方法

  • setMaxUrls($number)。设置单个文件中要写入的最大URL数量。默认值是50000,这是根据规范和大多数现有实现确定的限制。
  • setBufferSize($number)。设置在写入文件之前保留在内存中的URL数量。默认值是1000。如果您有更多内存,请考虑增加它。如果1000个URL不适用,请减少它。
  • setUseIndent($bool)。设置是否应该缩进XML。默认值是true。

运行测试

为了运行测试,执行以下命令

composer install
phpunit