paritoshbh/sitemap

生成网站地图和网站地图索引工具

2.2.2 2018-12-28 14:34 UTC

This package is auto-updated.

Last update: 2024-09-29 01:38:05 UTC


README

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

特性

  • 创建网站地图文件:可以是常规的或压缩的。
  • 创建多语言网站地图文件。
  • 创建网站地图索引文件。
  • 使用自定义样式表。
  • 当达到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');

// set stylesheet
$index->setStylesheet('http://example.com/css/sitemap.xsl');

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

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

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

多语言网站地图

use samdark\sitemap\Sitemap;

// create sitemap
// be sure to pass `true` as second parameter to specify XHTML namespace
$sitemap = new Sitemap(__DIR__ . '/sitemap_multi_language.xml', true);

// Set URL limit to fit in default limit of 50000 (default limit / number of languages) 
$sitemap->setMaxUrls(25000);

// add some URLs
$sitemap->addItem('http://example.com/mylink1');

$sitemap->addItem([
    'ru' => 'http://example.com/ru/mylink2',
    'en' => 'http://example.com/en/mylink2',
], time());

$sitemap->addItem([
    'ru' => 'http://example.com/ru/mylink3',
    'en' => 'http://example.com/en/mylink3',
], time(), Sitemap::HOURLY);

$sitemap->addItem([
    'ru' => 'http://example.com/ru/mylink4',
    'en' => 'http://example.com/en/mylink4',
], time(), Sitemap::DAILY, 0.3);

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

选项

有方法可以配置 Sitemap 实例

  • setMaxUrls($number). 设置单个文件中要写入的最大URL数量。默认为50000,这是规范和大多数现有实现的限制。
  • setMaxBytes($number). 设置单个网站地图文件的最大大小。默认为10MiB,应该与大多数当前搜索引擎兼容。
  • setBufferSize($number). 设置在写入文件之前保留在内存中的URL数量。默认为10。更大的值会带来微小的优势。另一方面,当达到文件大小限制时,必须将整个缓冲区写入下一个文件。
  • setUseIndent($bool). 设置是否缩进XML。默认为true。
  • setUseGzip($bool). 设置结果网站地图文件是否压缩。默认为false。必须启用zlib扩展才能使用此功能。
  • setStylesheet($string). 设置xml-stylesheet标签。默认情况下,标签不会生成。

有一个方法可以配置Index实例

  • setUseGzip($bool). 设置结果索引文件是否压缩。默认为false。必须启用zlib扩展才能使用此功能。
  • setStylesheet($string). 设置xml-stylesheet标签。默认情况下,标签不会生成。

运行测试

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

composer install
./vendor/bin/phpunit