veiliglanceren / laravel-seo-sitemap
Laravel Sitemap package to optimize your website in search engines
Requires
- ext-dom: *
- ext-simplexml: *
- illuminate/support: ^12.4
- laravel/framework: ^12.4
- scrumble-nl/popo: ^1.3
Requires (Dev)
- orchestra/testbench: ^10.1
- pestphp/pest: ^3.8
- pestphp/pest-plugin-laravel: ^3.1
- phpunit/phpunit: ^11.5
- dev-main
- 1.5.0
- 1.4.0
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- dev-@feature/v1.5.0
- dev-@fix/autoload-sitemap-head-helper
- dev-@feature/url-providers
- dev-@feature/sitemap-pinging
- dev-@feature/meta-tag-helper
- dev-@feature/sitemap-index
- dev-@fix/route-test
- dev-@feature/change-freq-macro
This package is not auto-updated.
Last update: 2025-05-21 08:33:16 UTC
README
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
docs/sitemap.md
docs/url.md
docs/image.md
docs/sitemapindex.md
docs/dynamic-routes.md
docs/template.md
๐ Folder Structure
src/
โ Core sitemap logictests/
โ Pest feature & unit testsdocs/
โ Documentationroutes/
โ Laravel route macrosdatabase/
โ Optional migrations
๐ License
MIT ยฉ VeiligLanceren.nl