DEV Community

MindWareZone
MindWareZone

Posted on

How to Generate a Sitemap in Laravel: A Complete Guide

Search engines like Google and Bing use sitemaps to efficiently crawl and index the pages of your website. In Laravel, generating a sitemap is easy with the help of the popular Spatie Sitemap package.

In this tutorial, you'll learn how to create a sitemap in Laravel using the Spatie Sitemap package.

What is a Sitemap?

A sitemap is an XML file that contains a list of your website's URLs. It helps search engines:

Discover newly published pages
Understand your site's structure
Crawl and index content more efficiently
Improve your website's SEO visibility

A typical sitemap URL looks like this:

https://yourwebsite.com/sitemap.xml

Step 1: Install the Sitemap Package

We'll use the excellent package provided by Spatie.

Run the following command:

composer require spatie/laravel-sitemap 
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Controller for Sitemap Generation

php artisan make:controller GenerateSitemap
Enter fullscreen mode Exit fullscreen mode

Step 3: Open the controller and add the following code.

namespace App\Http\Controllers; 

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

class GenerateSitemap extends Controller 
{ 
    public function index(){ 
        $sitemap = Sitemap::create(); 

        /* 
        -------------------------------------------------------------------------- 
         Static Pages 
        -------------------------------------------------------------------------- 
        */ 
        $sitemap->add( 
            Url::create('/') 
                ->setPriority(1.0) 
                ->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY) 
        ); 

        /* 
        -------------------------------------------------------------------------- 
         Posts 
        -------------------------------------------------------------------------- 
        */ 
        Post::with('category') 
            ->where('status', 1) 
            ->latest()->get() 
            ->each(function ($post) use ($sitemap) { 
            $sitemap->add( 
                Url::create($post?->category->slug .'/'. $post->slug) 
                    ->setLastModificationDate($post->updated_at) 
                    ->setPriority(0.9) 
                    ->setChangeFrequency(Url::CHANGE_FREQUENCY_WEEKLY) 
            ); 
        });         

        /* 
        |-------------------------------------------------------------------------- 
        | Write File 
        |-------------------------------------------------------------------------- 
        */ 
        $sitemap->writeToFile(public_path('sitemap.xml')); 

        return back()->with('success', 'Sitemap Generated Successfully!'); 
    } 
} 
Enter fullscreen mode Exit fullscreen mode

Read the full tutorial:

How to Generate a Sitemap in Laravel: A Complete Guide

Learn how to generate an XML sitemap in Laravel using the Spatie Sitemap package. Create dynamic sitemaps, add database URLs, automate updates with Laravel Scheduler, and improve your website's SEO and search engine indexing.

favicon mindwarezone.com

Top comments (0)