veiliglanceren/laravel-seo-sitemap

Laravel Sitemap package to optimize your website in search engines

1.5.0 2025-05-07 08:20 UTC

README

Latest Version on Packagist Total Downloads Static Badge Static Badge

Veilig Lanceren

This package is maintained by VeiligLanceren.nl, your partner in website development and everything else to power up your online company.

Laravel SEO Sitemap

A lightweight and extensible sitemap generator for Laravel that supports automatic route discovery, dynamic and static URL entries, and XML generation โ€” designed for SEO optimization.

๐Ÿš€ Features

  • ๐Ÿ” Automatic sitemap generation from named routes via ->sitemap() macro
  • ๐Ÿงฉ Model dynamic route support via ->sitemapUsing(Model::class) macro
  • ๐Ÿ” Template dynamic route support via ->sitemapUsing(SitemapItemTemplate::class) macro
  • ๐Ÿ“ฆ Dynamic route support via ->dynamic() macro
  • โœ๏ธ Customize entries with lastmod, priority, changefreq
  • ๐Ÿงผ Clean and compliant XML output
  • ๐Ÿ’พ Store sitemaps to disk or serve via route
  • ๐Ÿ›  Artisan command for lastmod updates
  • โœ… Fully tested using Pest and Laravel Testbench
  • ๐ŸŒ Default /sitemap.xml route included

๐Ÿ“ฆ Installation

composer require veiliglanceren/laravel-seo-sitemap

Run the installer to publish the route stub and wire it into routes/web.php:

php artisan sitemap:install

โš™๏ธ Configuration

If you're not using Laravel package auto-discovery, register the provider manually:

// bootstrap/providers.php
return [
    VeiligLanceren\LaravelSeoSitemap\SitemapServiceProvider::class,
];

Then publish the config file:

php artisan vendor:publish --tag=sitemap-config

And optionally publish & run the migration:

php artisan vendor:publish --tag=sitemap-migration
php artisan migrate

๐Ÿงญ Usage

๐Ÿ“„ Static Route

use VeiligLanceren\LaravelSeoSitemap\Support\Enums\ChangeFrequency;

Route::get('/contact', [ContactController::class, 'index'])
    ->name('contact')
    ->sitemap()
    ->changefreq(ChangeFrequency::WEEKLY)
    ->priority('0.8');

๐Ÿงฉ Template / Model Driven Route

use App\Sitemap\ItemTemplates\PostTemplate;

Route::get('/blog/{slug}', BlogController::class)
    ->name('blog.show')
    ->sitemapUsing(PostTemplate::class);

You may also point directly to an Eloquent model. The package will iterate over all() and generate URLs for each model instance:

Route::get('/product/{product}', ProductController::class)
    ->name('product.show')
    ->sitemapUsing(\App\Models\Product::class);

๐Ÿ”„ Dynamic Route

use VeiligLanceren\Sitemap\Dynamic\StaticDynamicRoute;
use VeiligLanceren\Sitemap\Dynamic\DynamicRouteChild;

Route::get('/blog/{slug}', BlogController::class)
    ->name('blog.show')
    ->dynamic(fn () => new StaticDynamicRoute([
        DynamicRouteChild::make(['slug' => 'first-post']),
        DynamicRouteChild::make(['slug' => 'second-post']),
    ]));

Generate Sitemap from Routes

php artisan sitemap:generate

Or via code:

use VeiligLanceren\LaravelSeoSitemap\Facades\Sitemap;

$sitemap = Sitemap::fromRoutes()->getSitemap();
$sitemap->save('sitemap.xml', 'public');

Sitemap::fromRoutes() returns a VeiligLanceren\LaravelSeoSitemap\Sitemap\Sitemap containing the object data of the sitemap.

๐Ÿ–ผ Add Images to URLs

use VeiligLanceren\LaravelSeoSitemap\Sitemap\Item\Url;
use VeiligLanceren\LaravelSeoSitemap\Sitemap\Item\Image;

$url = Url::make('https://example.com')
    ->addImage(Image::make('https://example.com/image1.jpg')->title('Hero 1'))
    ->addImage(Image::make('https://example.com/image2.jpg')->title('Hero 2'));

๐Ÿ—‚ Sitemap Index Support

use VeiligLanceren\LaravelSeoSitemap\Sitemap\SitemapIndex;

$sitemapIndex = SitemapIndex::make([
    'https://example.com/sitemap-posts.xml',
    'https://example.com/sitemap-pages.xml',
]);

Storage::disk('public')->put('sitemap.xml', $sitemapIndex->toXml());

๐Ÿ” Change Frequencies

Use ChangeFrequency enum values:

  • ALWAYS
  • HOURLY
  • DAILY
  • WEEKLY
  • MONTHLY
  • YEARLY
  • NEVER
->changefreq(ChangeFrequency::WEEKLY)

๐Ÿ›  Update lastmod

php artisan url:update contact

This sets the lastmod for the route to the current timestamp.

๐Ÿ”— Meta Tag Helper

<head>
    {!! Sitemap::meta() !!}
</head>

Outputs:

<link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml" />

๐Ÿงช Testing

vendor/bin/pest

SQLite must be enabled for in-memory testing.

๐Ÿ“š Documentation

๐Ÿ“‚ Folder Structure

  • src/ โ€“ Core sitemap logic
  • tests/ โ€“ Pest feature & unit tests
  • docs/ โ€“ Documentation
  • routes/ โ€“ Laravel route macros
  • database/ โ€“ Optional migrations

๐Ÿ“„ License

MIT ยฉ VeiligLanceren.nl