# How to Generate a Sitemap in Laravel Using Spatie Laravel Sitemap Package

By Dinesh Uprety
Published: 2024-10-15
Updated: 2025-05-06
Canonical: https://laranepal.com/blog/how-to-generate-a-sitemap-in-laravel-using-spatie-laravel-sitemap-package

# Step-by-Step Guide to Generating a Sitemap in Laravel with Spatie

Creating a sitemap is essential for improving your website's **`SEO`** and ensuring that search engines can easily index
your
content. In this article, we'll walk you through the steps to generate a sitemap for your Laravel application using the
[Spatie Laravel Sitemap package](https://github.com/spatie/laravel-sitemap).

## Step 1: Install the Spatie Laravel Sitemap Package

To get started, you need to install the Spatie Laravel Sitemap package. You can do this by running the following
command:

```bash
composer require spatie/laravel-sitemap
```

## Step 2: Publish the Configuration File

Next, you need to publish the configuration file for the package. You can do this by running the following command:

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

## Step 3: Create a Sitemap Generation Class

To generate a sitemap, you need to create a class that extends the `Spatie\Sitemap\Sitemap` class. This class
will be responsible for generating the sitemap content.

In this example, we'll create a class called BuildSitemap in the `App\Actions` namespace:

```php
<?php

namespace App\Actions;

use App\Models\Category;
use App\Models\Post;
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;

class BuildSitemap
{
    public function build(): void
    {
        Sitemap::create()
            ->add($this->build_index(Post::published()
                ->visible()
                ->get(), 'sitemap_articles.xml'))
            ->add($this->build_index(Category::visible()->get(), 'sitemap_categories.xml'))
            ->add(Url::create('/')->setPriority(1)->setChangeFrequency(URL::CHANGE_FREQUENCY_ALWAYS))
            ->add(Url::create('/newsletter')->setPriority(0.5)->setChangeFrequency(URL::CHANGE_FREQUENCY_MONTHLY))
            ->add(Url::create('/about')->setPriority(0.5)->setChangeFrequency(URL::CHANGE_FREQUENCY_MONTHLY))
            ->add(Url::create('/contact')->setPriority(0.5)->setChangeFrequency(URL::CHANGE_FREQUENCY_MONTHLY))
            ->add(Url::create('/ads')->setPriority(0.5)->setChangeFrequency(URL::CHANGE_FREQUENCY_MONTHLY))
            ->add(Url::create('/aboutme')->setPriority(0.5)->setChangeFrequency(URL::CHANGE_FREQUENCY_MONTHLY))
            ->writeToFile(public_path('sitemap.xml'));
    }

    protected function build_index($model, $path): string
    {
        Sitemap::create()
            ->add($model)
            ->writeToFile(public_path($path));

        return $path;
    }
}
```

You need to add your models directly by implementing the `\Spatie\Sitemap\Contracts\Sitemapable` interface.

```php
use Spatie\Sitemap\Contracts\Sitemapable;
use Spatie\Sitemap\Tags\Url;

class Post extends Model implements Sitemapable
{
    public function toSitemapTag(): Url | string | array
    {
        // Return with fine-grained control:
        return Url::create(route('blog.post.show', $this))
            ->setLastModificationDate(Carbon::create($this->updated_at));
    }
}

```

_note: This example was from my personal project, you can customize it to fit your needs. you can add more routes to the
sitemap. you can also add more models to the sitemap. i have added a sitemap for articles and categories._

## Step 4: Run the Sitemap Generation

To generate the sitemap, you need to call the `build` method on the `BuildSitemap` class. You can do this from a
controller, a command, or any other part of your application where you want to trigger the sitemap generation.

For example, you can create an Artisan command to generate the sitemap:

```php
<?php

namespace App\Console\Commands;

use App\Actions\BuildSitemap;
use Illuminate\Console\Command;

class GenerateSitemap extends Command
{
    protected $signature = 'sitemap:generate';

    protected $description = 'Generate the sitemap for the application';

    public function handle(BuildSitemap $buildSitemap): int
    {
        $buildSitemap->build();

        $this->info('Sitemap generated successfully.');

        return 0;
    }
}
```

You can then run the command to generate the sitemap:

```bash
php artisan sitemap:generate
```

## Conclusion

Generating a sitemap for your Laravel application is essential for improving your website's SEO and ensuring that search
engines can easily index your content. By following the steps outlined in this article, you can create a sitemap for
your application using the [Spatie Laravel Sitemap package](https://github.com/spatie/laravel-sitemap).

I hope you found this article helpful. If you have any questions or feedback, feel free to leave a comment below.

Happy coding! 🚀


