The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

Laravel Sitemap

spatie/laravel-sitemap image

Laravel Sitemap stats

Downloads
5.6M
Stars
2,034
Open Issues
7
Forks
261

View on GitHub →

Create and generate sitemaps with ease

Laravel Sitemap Package by Spatie

The Laravel Sitemap package by Spatie provides a seamless way to generate sitemaps for your Laravel application, ensuring your website's pages are indexed correctly by search engines. It offers a variety of methods to create and customize sitemaps, including dynamic crawling, manual additions, and integration with model data.

Key Features

  • Automatic Crawling: Generate sitemaps by crawling your website, automatically including all accessible URLs.
  • Manual Sitemap Creation: Manually add URLs to your sitemap, with options to set properties such as change frequency, last modification date, and priority.
  • Hybrid Approach: Start with an automatically generated sitemap and enhance it by adding more URLs manually.
  • JavaScript Execution: Capable of executing JavaScript to ensure that dynamically generated links are included.
  • Model Integration: Easily add URLs from Eloquent models by implementing the Sitemapable interface.
  • Customization Options: Control crawl behavior, including depth and exclusions, through a configurable crawler.
  • Storage Flexibility: Write sitemaps directly to various filesystems supported by Laravel.
  • Scheduled Sitemap Updates: Utilize Laravel's task scheduling to keep sitemaps up-to-date automatically.

Installation

Install the package via Composer:

composer require spatie/laravel-sitemap

The package will auto-register itself. For custom configurations, publish the configuration file:

php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config

Usage

Generate a sitemap with all links from a domain:

use Spatie\Sitemap\SitemapGenerator;
 
SitemapGenerator::create('https://example.com')->writeToFile($path);

Add URLs manually:

use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
 
Sitemap::create()
->add(Url::create('/home')->setPriority(0.1))
->writeToFile($path);

For more complex scenarios, such as adding model data or combining crawled and manually added URLs, refer to the detailed documentation in the package.

Configuration

After publishing, adjust settings in the config/sitemap.php to customize crawler behavior, including request timeouts, cookie handling, and JavaScript execution settings.

Automating Sitemap Generation

Set up a scheduled task in Laravel to regenerate the sitemap periodically, ensuring it remains up-to-date as your site's content changes.

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('sitemap:generate')->daily();
}

Conclusion

The Spatie Laravel Sitemap package is a powerful tool for automating the management of sitemaps, essential for SEO. With comprehensive customization options and easy integration, it is an invaluable addition to any Laravel application. For further details, visit the official repository.

spatie photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Laravel Sitemap Related Articles

Lens for Laravel Brings WCAG Auditing to Your Local Dev Workflow image

Lens for Laravel Brings WCAG Auditing to Your Local Dev Workflow

Read article
Building a Sitemap in your Laravel app with the Spatie Sitemap image

Building a Sitemap in your Laravel app with the Spatie Sitemap

Read article
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger