miraclebg / php-sitemap-generator
简单的PHP网站地图生成器。
2.0.0
2020-01-09 11:24 UTC
Requires
- php: ^7.2
- ext-curl: *
- ext-dom: *
- ext-mbstring: *
- ext-simplexml: *
- ext-spl: *
- ext-zlib: *
Requires (Dev)
- php-mock/php-mock-phpunit: ^2.5
- phpunit/phpunit: ^8.5
README
网站地图生成和提交的库。
内部使用SplFixedArrays,因此比替代品更快,内存使用更少。
特性
- 遵循sitemaps.org协议
- 支持多语言页面的替代链接(见google文档)
使用示例
<?php include "src/SitemapGenerator.php"; // Setting the current working directory to be output directory // for generated sitemaps (and, if needed, robots.txt) // The output directory setting is optional and provided for demonstration purpose. // By default output is written to current directory. $outputDir = getcwd(); $generator = new \Icamys\SitemapGenerator\SitemapGenerator('example.com', $outputDir); // will create also compressed (gzipped) sitemap $generator->toggleGZipFileCreation(); // determine how many urls should be put into one file; // this feature is useful in case if you have too large urls // and your sitemap is out of allowed size (50Mb) // according to the standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html) $generator->setMaxURLsPerSitemap(50000); // sitemap file name $generator->setSitemapFileName("sitemap.xml"); // sitemap index file name $generator->setSitemapIndexFileName("sitemap-index.xml"); // alternate languages $alternates = [ ['hreflang' => 'de', 'href' => "http://www.example.com/de"], ['hreflang' => 'fr', 'href' => "http://www.example.com/fr"], ]; // adding url `loc`, `lastmodified`, `changefreq`, `priority`, `alternates` $generator->addURL('http://example.com/url/path/', new DateTime(), 'always', 0.5, $alternates); // generate internally a sitemap $generator->createSitemap(); // write early generated sitemap to file(s) $generator->writeSitemap(); // update robots.txt file in output directory or create a new one $generator->updateRobots(); // submit your sitemaps to Google, Yahoo, Bing and Ask.com $generator->submitSitemap();
测试
使用命令运行测试
$ ./vendor/bin/phpunit
运行代码覆盖率
$ ./vendor/bin/phpunit --coverage-html ./coverage
变更日志
2.0.0版本新增
- 主要代码重构
- 生成器中不再有公共属性,只使用方法
- 为了使用
addUrl
移除了addUrls
方法 - 修复了robots.txt更新中的bug
- 修复了addURL方法中的bug(空loc)
- 添加了单元测试以确保质量
- 根据网站地图规范更新了限制
- 更新了搜索引擎的URL
- 添加了更改频率验证