Git Product home page Git Product logo

oc-sitemap-plugin's Introduction

Vdlp.Sitemap

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

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 [email protected].

Contribution

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

oc-sitemap-plugin's People

Contributors

adrenth avatar jacobdekeizer avatar sander-beenen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oc-sitemap-plugin's Issues

CMS links disappear

Hi.
I've created my own generator for a small 'jobs' plugin. The links appear fine in the sitemap, but the CMS pages disappear. If I disable the sitemap for my plugin the CMS pages appear again.

My class...

namespace Acme\Jobs\Classes;

use Carbon\Carbon;
use Illuminate\Contracts\Routing\UrlGenerator;
use Psr\Log\LoggerInterface;
use Throwable;
use Vdlp\Sitemap\Classes\Contracts\DefinitionGenerator;
use Vdlp\Sitemap\Classes\Dto;

final class JobsPagesGenerator implements DefinitionGenerator
{
    public function getDefinitions(): Dto\Definitions
    {
        $definitions = new Dto\Definitions();
        for ($i = 0; $i < 10; $i++) {
            $definitions->addItem(
                (new Dto\Definition)->setModifiedAt(Carbon::now())
                    ->setPriority(1)
                    ->setUrl(url("jobs/".$i) )
                    ->setChangeFrequency(Dto\Definition::CHANGE_FREQUENCY_ALWAYS)
            );
        }

        return $definitions;
    }
}

Any help very much appreciated. Thank-you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.