sebacarrasco93 / laravel-simple-sitemap
Laravel Simple Sitemap
Package info
github.com/sebacarrasco93/laravel-simple-sitemap
pkg:composer/sebacarrasco93/laravel-simple-sitemap
Fund package maintenance!
Requires
- php: ^8.2
- illuminate/contracts: ^11.0||^12.0||^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1
- orchestra/testbench: ^9.0||^10.0||^11.0
- pestphp/pest: ^3.0||^4.0
- pestphp/pest-plugin-arch: ^3.0||^4.0
- pestphp/pest-plugin-laravel: ^3.0||^4.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
This package is auto-updated.
Last update: 2026-04-27 03:05:43 UTC
README
A very simple package: Create sitemaps "on the fly"
Installation
You can install the package via composer:
composer require sebacarrasco93/laravel-simple-sitemap
You can publish the config file with:
php artisan vendor:publish --tag="simple-sitemap-config"
This is the contents of the published config file:
return [ 'default_frequency' => 'monthly', 'default_priority' => '0.50', ];
Usage
Create a sitemap for Eloquent Collections
Let's assume that you want to make a sitemap of all the categories, you can do that in only 3 steps!
// app/Models/Category use SebaCarrasco93\SimpleSitemap\Traits\SimpleSitemapCollection; // 👈 1: Import Trait class Category extends Model { use HasFactory; // ... use SimpleSitemapCollection; // 👈 2: Use the trait // ... // 👇 Step 3: Create getSitemapUrlAttribute() method and specify the full url public function getSitemapUrlAttribute(): string { return route('category.show', $this); } }
Now, you can use it
// web.php, controller or equivalent $categories = Category::get(); return SimpleSitemap::fromEloquentCollection($categories);
Can I short the syntax? Of course!
return Category::sitemap(); // Equivalent to SimpleSitemap::fromEloquentCollection(Category::get());
Advanced usage
A sitemap for only active categories? Sure!
return Category::where('active', true) ->sitemap();
A sitemap for active, in desc order and paginate? It's Eloquent and Laravel!
$paginated_active_categories = Category::where('active', true) ->orderBy('desc', 'id') ->paginate(); return SimpleSitemap::fromEloquentCollection($paginated_active_categories);
Easy Peasy!
Add to a specific routes
You can add to a custom routes in only 2 steps:
Adding the Middlewares
// app/Http/Kernel.php // ... protected $middlewareAliases = [ // ... 'sitemap' => \SebaCarrasco93\SimpleSitemap\Middleware\SimpleSitemap::class, ];
Using the Middleware:
You can add all your get routes. If you add another such as post, patch, put, etc. it will be ignored.
// web.php or equivalent Route::get('your-route', [YourController::class]) ->middleware('sitemap'); // 👈 Route::get('your-route', function () { return 'It works with a closure, too'; })->middleware('sitemap'); // 👈
Advanced
If you don't wanna to add each one, you can add into your all php
// app/Providers/RouteServiceProvider.php Route::middleware(['web', 'sitemap']) // 👈 ->group(base_path('routes/web.php'));
Also, if you want to exclude a specific route from a group, you can add sitemap:exclude middleware
// web.php or equivalent Route::get('to_exclude', function () { // You very important route to exclude })->middleware('sitemap:exclude');
Now, you can see all your routes
return SimpleSitemap::routes();
Creating a index
Optionally, you can create a index sitemap with your sitemap collections
$routes = [ route('sitemaps/index-1'), // You can pass it as a route 'https://yourdomain.com/sitemaps/index-2', // or, as full path '/sitemaps/index-3', // as a relative path, too ]; return SimpleSitemap::index($routes);
Custom frequency and priority
If you want to customize each frequency or priority, you can add a migration
php artisan make:migration add_sitemap_columns_to_{your_table}_table
$table->addColumn('string', 'frequency')->nullable(); $table->addColumn('string', 'priority')->nullable();
You can also specify a custom frequency for a specific model
// app/Models/YourModel.php public function getFrequencyAttribute(?string $frequency = null): string { return $frequency ?? 'weekly'; }
Specify custom frequency to a specific model
// app/Models/YourModel.php public function getPriorityAttribute(?string $priority = null): string { return $priority ?? '0.1'; }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.