sergeybruhin/xml-sitemap

Laravel XML Sitemap Package

1.0.2 2023-01-07 18:56 UTC

This package is auto-updated.

Last update: 2024-11-04 20:15:00 UTC


README

Latest Version on Packagist Total Downloads

Basic and simple package to help you generate XML sitemap.

Installation

You can install the package via composer:

composer require sergeybruhin/xml-sitemap

Compose and store Sitemaps in a Laravel command or wherever you want

<?php

namespace App\Console\Commands;

use App\Models\Page;
use Illuminate\Console\Command;
use SergeyBruhin\XmlSitemap\Dto\XmlSitemapImage;
use SergeyBruhin\XmlSitemap\Dto\XmlSitemapSitemap;
use SergeyBruhin\XmlSitemap\Dto\XmlSitemapUrl;
use SergeyBruhin\XmlSitemap\Facades\XmlSitemap;

class GenerateSitemapCommand extends Command
{
    protected $signature = 'generate:sitemap';

    protected $description = 'Generate sitemap';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle(): int
    {
        $sitemapIndex = XmlSitemap::createIndex();
        $sitemapIndex->addSitemap($this->createPagesSitemap());

        XmlSitemap::storeIndex($sitemapIndex);
        foreach ($sitemapIndex->sitemaps as $sitemap) {
            XmlSitemap::storeSitemap($sitemap);
        }

        return 1;

    }

    private function createPagesSitemap(): XmlSitemapSitemap
    {
        $sitemap = XmlSitemap::createSitemap('pages_sitemap.xml');

        Page::each(function (Page $page) use ($sitemap) {
            $url = new XmlSitemapUrl(route('page', $page->slug), 0.8);
            $url->setLastModificationDate($page->updated_at);
            $url->setFrequency(XmlSitemapUrl::WEEKLY);
            $url->addAlternate(route('page', $page->slug) . '/en', 'en');
            $url->addImage((new XmlSitemapImage('https://some/image/url.png')));

            $sitemap->addUrl($url);
        });
        return $sitemap;
    }
}

Run Command via scheduler every "period of time"

  protected function schedule(Schedule $schedule): void
    {
        $schedule->command('generate:sitemap')->hourly();
    }

Testing (Not yet 💁‍♂️)

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email sundaycreative@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.