fuelviews / laravel-sitemap
Laravel sitemap package
Installs: 1 160
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.3
- spatie/crawler: ^8.4.0
- spatie/laravel-package-tools: ^1.92
- spatie/laravel-sitemap: ^7.3
Requires (Dev)
- driftingly/rector-laravel: ^2.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.2||^9.0.0||^8.22.0
- pestphp/pest: ^3.0||^2.34
- pestphp/pest-plugin-arch: ^3.0||^2.7
- pestphp/pest-plugin-laravel: ^3.2||^2.3
- rector/rector: ^2.0
This package is auto-updated.
Last update: 2025-08-20 22:54:42 UTC
README
Laravel Sitemap is a robust and intelligent solution for automatically generating XML sitemaps for your Laravel application. Built on top of Spatie's excellent sitemap package, it provides advanced crawling capabilities, model integration, and flexible configuration options.
Requirements
- PHP ^8.3
- Laravel ^10.0 || ^11.0 || ^12.0
- Spatie Sitemap ^2.0
- Spatie Crawler ^7.0
Installation
Install the package via Composer:
composer require fuelviews/laravel-sitemap
Publish the configuration file:
php artisan vendor:publish --tag="sitemap-config"
This will create a config/fv-sitemap.php
file where you can customize your sitemap settings.
Basic Usage
Generate Sitemap
Generate your sitemap using the Artisan command:
php artisan sitemap:generate
Access Your Sitemap
Once generated, your sitemap will be available at:
https://yoursite.com/sitemap.xml
Using the Facade
use Fuelviews\Sitemap\Facades\Sitemap; // Get sitemap content (generates if not exists) $content = Sitemap::getSitemapContents('sitemap.xml');
In Blade Templates
Link to your sitemap in templates:
<link rel="sitemap" type="application/xml" title="Sitemap" href="{{ route('sitemap') }}" />
Configuration
Basic Configuration
The main configuration options in config/fv-sitemap.php
:
return [ // Storage disk for sitemap files 'disk' => env('SITEMAP_DISK', 'public'), // Generate sitemap index for large sites 'exclude_subcategory_sitemap_links' => env('SITEMAP_USE_INDEX', true), // Exclude redirect URLs 'exclude_redirects' => env('SITEMAP_EXCLUDE_REDIRECTS', true), // Routes to exclude 'exclude_route_names' => [ // 'admin.*', // 'api.*', ], // Paths to exclude 'exclude_paths' => [ // '/admin', // '/dashboard', ], // Specific URLs to exclude 'exclude_urls' => [ '/sitemap.xml', '/pages_sitemap.xml', '/posts_sitemap.xml', ], // Models to include in posts sitemap 'post_model' => [ // App\Models\Post::class, ], ];
Environment Variables
You can use environment variables for common settings:
# .env file SITEMAP_DISK=public SITEMAP_USE_INDEX=true SITEMAP_EXCLUDE_REDIRECTS=true
Advanced Usage
Model Integration
To include your Eloquent models in the sitemap, implement the Sitemapable
interface:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Spatie\Sitemap\Contracts\Sitemapable; use Spatie\Sitemap\Tags\Url; class Post extends Model implements Sitemapable { /** * Convert the model instance into a sitemap URL entry. */ public function toSitemapUrl(): Url { return Url::create(route('posts.show', $this)) ->setLastModificationDate($this->updated_at) ->setChangeFrequency('weekly') ->setPriority(0.8); } }
Then add your model to the configuration:
// config/fv-sitemap.php 'post_model' => [ App\Models\Post::class, App\Models\Article::class, ],
Sitemap Types
Single Sitemap (Default)
When exclude_subcategory_sitemap_links
is false
, generates one sitemap containing all content:
- All crawlable pages
- All configured model entries
Sitemap Index
When exclude_subcategory_sitemap_links
is true
, generates:
sitemap.xml
- Sitemap index filepages_sitemap.xml
- All crawled pagesposts_sitemap.xml
- All model entries
Exclusion Rules
By Route Names
Exclude specific Laravel routes:
'exclude_route_names' => [ 'admin.dashboard', 'api.users.index', 'auth.*', // Wildcard support ],
By URL Paths
Exclude URL paths (supports prefixes):
'exclude_paths' => [ '/admin', // Excludes /admin, /admin/users, etc. '/dashboard', // Excludes /dashboard and sub-paths '/api', // Excludes all API endpoints ],
By Exact URLs
Exclude specific URLs:
'exclude_urls' => [ '/login', '/register', '/sitemap.xml', // Don't include sitemap in itself ],
Custom Storage
Use different storage disks:
// config/fv-sitemap.php 'disk' => 's3', // Store sitemaps on S3 // Or use environment variable 'disk' => env('SITEMAP_DISK', 'public'),
Automatic Generation
The package automatically generates sitemaps when they're requested but don't exist. This means:
- A user visits
/sitemap.xml
- If the sitemap doesn't exist, it's generated on-the-fly
- The generated sitemap is served immediately
- Future requests serve the cached version
Performance Considerations
Large Sites
For sites with many pages, use sitemap indexes:
'exclude_subcategory_sitemap_links' => true,
This creates separate sitemaps for pages and posts, improving crawl efficiency.
Crawler Configuration
The package uses Spatie's crawler with optimized settings:
- Ignores robots.txt for complete site mapping
- Filters out query parameters
- Normalizes URLs to prevent duplicates
- Excludes redirect URLs by default
Caching
- Generated sitemaps are stored on your configured disk
- HTTP responses include appropriate cache headers
- Sitemaps are only regenerated when missing
Command Reference
Generate Sitemap
php artisan sitemap:generate
Generates the complete sitemap structure based on your configuration.
Configuration Publishing
php artisan vendor:publish --tag="sitemap-config"
Publishes the configuration file to config/fv-sitemap.php
.
Testing
Run the package tests:
composer test
Run tests with coverage:
composer test-coverage
Troubleshooting
Common Issues
Sitemap Not Generating
- Check configuration: Ensure
config/fv-sitemap.php
is properly configured - Model issues: Verify models implement
Sitemapable
interface - Storage permissions: Ensure the configured disk is writable
- Route accessibility: Verify your application routes are accessible
Empty Sitemap
- Exclusion rules: Check if your exclusion rules are too broad
- Route registration: Ensure your routes are properly registered
- Model queries: Verify your models have
published
status records
Memory Issues
- Use sitemap indexes: Enable
exclude_subcategory_sitemap_links
- Limit model queries: Add additional constraints in your models
- Increase memory limit: Adjust PHP memory limit for generation
Debug Information
Enable debug logging by checking Laravel logs during generation:
tail -f storage/logs/laravel.log
The package logs detailed information about:
- Generation process
- Excluded URLs and reasons
- Model processing
- Storage operations
SEO Best Practices
URL Structure
- Use clean URLs without query parameters
- Implement proper canonical URLs
- Ensure consistent URL structure
Update Frequencies
Configure appropriate change frequencies in your models:
public function toSitemapUrl(): Url { return Url::create(route('posts.show', $this)) ->setLastModificationDate($this->updated_at) ->setChangeFrequency('weekly') // daily, weekly, monthly ->setPriority(0.8); // 0.0 to 1.0 }
Sitemap Registration
Register your sitemap with search engines:
- Google Search Console
- Bing Webmaster Tools
- Add
<link rel="sitemap">
to your HTML head
Contributing
Please see CONTRIBUTING for details.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Joshua Mitchener - Lead Developer
- Daniel Clark - Contributor
- Fuelviews - Organization
- Spatie - Underlying sitemap and crawler packages
- All Contributors
📜 License
The MIT License (MIT). Please see License File for more information.
Built with ❤️ by the Fuelviews team