vdlp / oc-sitemap-plugin
October CMS 的 sitemap.xml 生成器。
2.3.0
2023-05-31 13:46 UTC
Requires
- php: ^8.0.2
- composer/installers: ^1.0 || ^2.0
- october/rain: ^3.0
Suggests
- vdlp/oc-sitemapgenerators-plugin: Adds pre-built sitemap generators for your October CMS website.
This package is auto-updated.
Last update: 2024-09-14 15:01:59 UTC
README
Vdlp.Sitemap
此插件允许开发者使用 sitemap 定义生成器创建 sitemap.xml。
要求
- PHP 8.0.2 或更高版本
- 仅支持 October CMS 3.x
用法
Sitemap 定义生成器
要生成 sitemap 项,您可以创建自己的 sitemap 定义生成器。
示例
final class DefinitionGenerator implements Contracts\DefinitionGenerator { public function getDefinitions(): Definitions { $definitions = new Definitions(); for ($i = 0; $i < 100; $i++) { $definitions->addItem( (new Definition)->setModifiedAt(Carbon::now()) ->setPriority(1) ->setUrl('example.com/page/' . $i) ->setChangeFrequency(Definition::CHANGE_FREQUENCY_ALWAYS) ); } return $definitions; } }
在您的插件类中的 boot
方法中注册您的生成器
Event::listen(Contracts\SitemapGenerator::GENERATE_EVENT, static function(): DefinitionGenerator { return new DefinitionGenerator(); });
您也可以注册多个生成器
Event::listen(Contracts\SitemapGenerator::GENERATE_EVENT, static function(): array { return [ new DefinitionGeneratorOne(), new DefinitionGeneratorTwo(), // .. ]; });
无效化 sitemap 缓存
您可以通过触发事件来无效化 sitemap 缓存
Event::fire(Contracts\SitemapGenerator::INVALIDATE_CACHE_EVENT);
或解析生成器实例并使用无效化缓存方法
/** @var SitemapGenerator $sitemapGenerator */ $sitemapGenerator = resolve(Contracts\SitemapGenerator::class); $sitemapGenerator->invalidateCache();
更新 / 添加 / 删除缓存中的定义
首先解析 sitemap 生成器
/** @var SitemapGenerator $sitemapGenerator */ $sitemapGenerator = resolve(Contracts\SitemapGenerator::class);
添加定义
$sitemapGenerator->addDefinition( (new Definition()) ->setUrl('example.com/new-url') ->setModifiedAt(Carbon::now()) ->setChangeFrequency(Definition::CHANGE_FREQUENCY_YEARLY) ->setPriority(5) );
更新定义
注意,定义是通过它们的 URL 更新的。
$sitemapGenerator->updateDefinition( (new Definition()) ->setUrl('example.com/page/1') ->setModifiedAt(Carbon::parse('1900-10-10')) ->setPriority(7) ->setChangeFrequency(Definition::CHANGE_FREQUENCY_HOURLY), 'example.com/page/0' // (optional) specify the url to update in cache, when old url is null the definition url will be used. );
更新或添加定义
$sitemapGenerator->updateOrAddDefinition( (new Definition()) ->setUrl('example.com/create-or-add') ->setModifiedAt(Carbon::now()) ->setChangeFrequency(Definition::CHANGE_FREQUENCY_YEARLY) ->setPriority(5), null // (optional) specify the url to update in cache, when old url is null the definition url will be used. );
删除定义
$sitemapGenerator->deleteDefinition('example.com/new-url');
从 sitemap 中排除 URL
Event::listen(SitemapGenerator::EXCLUDE_URLS_EVENT, static function (): array { return [ 'example.com/page/1', ]; });
配置
将插件配置添加到您的配置文件夹
php artisan vendor:publish --provider="Vdlp\Sitemap\ServiceProvider" --tag="config"
您可以在 .env
文件中更改 sitemap 缓存的秒数。您还可以将 sitemap 缓存永久保存。
VDLP_SITEMAP_CACHE_TIME=3600 VDLP_SITEMAP_CACHE_FOREVER=false
ConfigResolver
您可以选择在配置文件中提供自己的 ConfigResolver 实现来覆盖如何解析 sitemap 配置。这对于多站项目很有用,其中 sitemap 应该按域名缓存。
use Illuminate\Contracts\Config\Repository; use Illuminate\Http\Request; use Vdlp\Sitemap\Classes\Contracts\ConfigResolver; use Vdlp\Sitemap\Classes\Dto\SitemapConfig; final class MultisiteConfigResolver implements ConfigResolver { public function __construct(private Repository $config, private Request $request) { } public function getConfig(): SitemapConfig { $domain = $this->request->getHost(); return new SitemapConfig( 'vdlp_sitemap_cache_' . $domain, 'vdlp_sitemap_definitions_' . $domain, sprintf('vdlp/sitemap/sitemap_%s.xml', $domain), (int) $this->config->get('sitemap.cache_time', 3600), (bool) $this->config->get('sitemap.cache_forever', false) ); } }
问题
如果您在使用此插件时遇到问题,请在 GitHub 上创建一个问题,或通过 octobercms@vdlp.nl 联系我们。
贡献
任何帮助都受欢迎。或者您可以自由地在 GitHub 上创建一个 Pull Request。