webarchitect609 / sitemap
根据robots.txt中的disallow规则生成sitemap.xml,遵循https://www.sitemaps.org规范,且不依赖任何框架或CMS。
v0.1.1
2018-12-10 07:46 UTC
Requires
- php: ^7.0
- doctrine/cache: ^1.6
- doctrine/collections: ^1.4
- jms/serializer: ^1.12
- psr/log: ^1.0
- vipnytt/robotstxtparser: ^2.0
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is not auto-updated.
Last update: 2024-09-25 10:00:52 UTC
README
注意:此版本可能不稳定,所有接口可能在近期内更改。
特性
- 遵守Sitemaps XML格式及其限制;
- Sitemapindex始终支持;
- 自动在sitemapindex中为sitemap设置'lastmod',使用其urls中最新的'lastmod';
- 自动为urls添加主机名;
- 遵守robots.txt的
Disallow
规则,以'*'代理帮助您避免在sitemap中出现不想要的urls; - 写入临时文件夹并部署到最终目的地,以防问题损坏旧版本;
- 不依赖任何框架;
如何使用
1 通过composer安装
composer require webarchitect609/sitemap
2 创建writer实例
$sitemapWriter = new \WebArch\Sitemap\SitemapWriter('http://example.org', '/var/www/example.org/htdocs');
3 对于简单使用,直接将urls添加到writer
use WebArch\Sitemap\Enum\ChangeFreq;
use WebArch\Sitemap\Model\Url;
$url1 = (new Url('/index.php'))->withChangefreq(ChangeFreq::CHANGE_FREQ_DAILY)
->withPriority(0.9)
->withLastmod(
DateTimeImmutable::createFromFormat(
'Y-m-d H:i:s',
'2018-07-13 10:37:48'
)
);
$url2 = (new Url('/news/index.php'))->withChangefreq(ChangeFreq::CHANGE_FREQ_HOURLY);
$sitemapWriter->addUrl($url1)
->addUrl($url2);
4 对于更复杂的情况,可以创建所需数量的sitemap并交给writer
$newsSitemap = new \WebArch\Sitemap\Model\Sitemap('/sitemap_news.xml');
$newsSitemap->addUrl(
(new Url('/news/detail/1/'))
)
->addUrl(
(new Url('/news/detail/2/'))
)
->addUrl(
(new Url('/news/detail/3/'))
);
$sitemapWriter->addSitemap($newsSitemap);
5 可以应用附加选项以遵守sitemap限制
/**
* Limit maximum urls count. When it's overflowed an `\WebArch\Sitemap\Exception\UrlCountLimitException` would be issued.
* It's ON by default.
*/
$newsSitemap->getUrlSet()->withMaxUrlCount(50000);
/**
* Limit maximum file size
* It's OFF by default.
* WARNING: it WILL slow down everything: after adding new url estimated size calculations would be executed.
* (Hope to get rid of this in the future)
*/
$newsSitemap->getUrlSet()->withMaxXmlSizeBytes(10*1024*1024);
6 然后让它工作
$sitemapWriter->write();
在此之后,所有内容都将写入sys_get_temp_dir()
。如果没有错误,新版本的sitemapindex和所有sitemap将被部署到其最终目的地$sitemapWriter->getBaseDir()
,并将文件权限更改为允许任何人读取。
运行单元测试
composer test