vdlp/oc-sitemap-plugin

A sitemap.xml generator for October CMS.

Installs: 6 207

Dependents: 1

Suggesters: 0

Security: 0

Stars: 6

Watchers: 9

Forks: 3

Open Issues: 0

Type:october-plugin

2.3.0 2023-05-31 13:46 UTC

This package is auto-updated.

Last update: 2024-04-14 14:10:04 UTC


README

68747470733a2f2f706c7567696e732e76646c702e6e6c2f6f63746f626572636d732f69636f6e732f56646c702e536974656d61702e737667

Vdlp.Sitemap

This plugin allows developers to create a sitemap.xml using a sitemap definition generator.

68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f7068702f76646c702f6f632d736974656d61702d706c7567696e 68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f6c6963656e73652f76646c702f6f632d736974656d61702d706c7567696e 68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f76646c702f6f632d736974656d61702d706c7567696e2f6c6174657374 68747470733a2f2f62616467656e2e6e65742f62616467652f636d732f4f63746f626572253230434d53 68747470733a2f2f62616467656e2e6e65742f62616467652f747970652f706c7567696e 68747470733a2f2f706c7567696e732e76646c702e6e6c2f6f63746f626572636d732f62616467652f696e7374616c6c6174696f6e732e7068703f706c7567696e3d76646c702d736974656d6170

Requirements

  • PHP 8.0.2 or higher
  • Supports October CMS 3.x only

Usage

Sitemap definitions generator

To generate sitemap items you can create your own sitemap definition generator.

Example:

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;
    }
}

Register your generator in the boot method of your plugin class:

Event::listen(Contracts\SitemapGenerator::GENERATE_EVENT, static function(): DefinitionGenerator {
    return new DefinitionGenerator();
});

You can also register multiple generators:

Event::listen(Contracts\SitemapGenerator::GENERATE_EVENT, static function(): array {
    return [
        new DefinitionGeneratorOne(),
        new DefinitionGeneratorTwo(),
        // ..
    ];
});

Invalidate sitemap cache

You can fire an event to invalidate the sitemap cache

Event::fire(Contracts\SitemapGenerator::INVALIDATE_CACHE_EVENT);

Or resolve the generator instance and use the invalidate cache method

/** @var SitemapGenerator $sitemapGenerator */
$sitemapGenerator = resolve(Contracts\SitemapGenerator::class);
$sitemapGenerator->invalidateCache();

Update / Add / Delete definitions in cache

First resolve the sitemap generator

/** @var SitemapGenerator $sitemapGenerator */
$sitemapGenerator = resolve(Contracts\SitemapGenerator::class);

Add definitions

$sitemapGenerator->addDefinition(
    (new Definition())
        ->setUrl('example.com/new-url')
        ->setModifiedAt(Carbon::now())
        ->setChangeFrequency(Definition::CHANGE_FREQUENCY_YEARLY)
        ->setPriority(5)
);

Update definitions

Note, definitions are updated by their 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.
);

Update or add definitions

$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.
);

Delete definitions

$sitemapGenerator->deleteDefinition('example.com/new-url');

Exclude URLs from sitemap

Event::listen(SitemapGenerator::EXCLUDE_URLS_EVENT, static function (): array {
    return [
        'example.com/page/1',
    ];
});

Configuration

Add the plugin configuration to your config folder:

php artisan vendor:publish --provider="Vdlp\Sitemap\ServiceProvider" --tag="config"

You can change the amount of seconds the sitemap is cached in your .env file. You can also cache the sitemap forever.

VDLP_SITEMAP_CACHE_TIME=3600
VDLP_SITEMAP_CACHE_FOREVER=false

ConfigResolver

Optionally you can override how the sitemap config should be resolved by giving your own ConfigResolver implementation in the config file. This can be useful for multisite projects, where the sitemap should be cached per domain.

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)
        );
    }
}

Issues

If you have issues using this plugin. Please create an issue on GitHub or contact us at octobercms@vdlp.nl.

Contribution

Any help is appreciated. Or feel free to create a Pull Request on GitHub.