artemperepechai / sitemap
网站地图和网站地图索引构建器
2.0.6
2016-10-04 07:11 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-23 13:38:52 UTC
README
网站地图和网站地图索引构建器。
特性
- 创建网站地图文件。
- 创建网站地图索引文件。
- 当达到50000个URL的限制时,自动创建新文件。
- 具有可配置大小的内存高效缓冲区。
安装
在composer.json中
"require": { "artemPerepechai/sitemap" : "dev-master" }, "repositories": [ { "url": "https://github.com/artemPerepechai/sitemap", "type": "vcs" } ]
之后,确保您的应用程序通过包含vendor/autoload.php
来自动加载Composer类。
如何使用
use samdark\sitemap\Sitemap; use samdark\sitemap\Index; // create sitemap $sitemap = new Sitemap(__DIR__ . '/sitemap.xml'); // add some URLs $sitemap->setLocation('http://example.com/mylink4') ->setLastModified(time()) ->setFrequency(Sitemap::DAILY) ->setAlternateLanguage('en', 'http://example.com/en') ->setAlternateLanguage('de', 'http://example.com/de') ->setAlternateLanguage('fr', 'http://example.com/fr') ->setPriority(0.1) ->addItem(); // 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->setLocation('http://example.com/about') ->setLastModified(time()) ->addItem(); $staticSitemap->setLocation('http://example.com/tos') ->setLastModified(time()) ->addItem(); $staticSitemap->setLocation('http://example.com/jobs') ->setLastModified(time()) ->addItem(); // 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();
选项
有两种方法可以配置Sitemap
实例
setMaxUrls($number)
。设置单个文件中要写入的最大URL数量。默认值是50000,这是根据规范以及大多数现有实现的限制。setBufferSize($number)
。设置在写入文件之前保留在内存中的URL数量。默认值是1000。如果您有更多内存,请考虑将其增加。如果1000个URL不适合,请减少它。setUseIndent($bool)
。设置是否对XML进行缩进。默认值是true。
运行测试
为了运行测试,请执行以下命令
composer install
phpunit