miraclebg/php-sitemap-generator

简单的PHP网站地图生成器。

2.0.0 2020-01-09 11:24 UTC

This package is auto-updated.

Last update: 2024-09-12 23:48:17 UTC


README

Build Status codecov.io Minimum PHP Version Latest Stable Version Total Downloads

网站地图生成和提交的库。

内部使用SplFixedArrays,因此比替代品更快,内存使用更少。

特性

使用示例

<?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
  • 添加了更改频率验证