PHP code example of vdlp / oc-sitemap-plugin

1. Go to this page and download the library: Download vdlp/oc-sitemap-plugin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vdlp / oc-sitemap-plugin example snippets


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

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(),
        // ..
    ];
});

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

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

/** @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)
);

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

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

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

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