toanld / sitemap
生成网站地图和网站地图索引的工具
dev-master
2018-10-24 02:51 UTC
Requires
- php: >=5.3.0
- ext-xmlwriter: *
Requires (Dev)
- phpunit/phpunit: ~4.4
This package is auto-updated.
Last update: 2024-09-17 13:53:26 UTC
README
生成网站地图和网站地图索引。
功能
- 创建网站地图文件:可以是普通文件或压缩文件。
- 创建多语言网站地图文件。
- 创建网站地图索引文件。
- 当达到URL限制或文件大小限制时,自动创建新文件。
- 快速且内存效率高。
安装
通过Composer安装非常简单
composer require toanld/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