phantoanit/sitemap

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

dev-master 2024-04-25 10:43 UTC

This package is auto-updated.

Last update: 2024-09-25 11:30:02 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');

// 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 扩展。

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

  • setUseGzip($bool). 设置生成的索引文件是否应压缩。默认为 false。使用此功能必须启用 zlib 扩展。

运行测试

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

composer install
./vendor/bin/phpunit