How to Generate Sitemap XML File in Laravel 9?

Published On: 25/03/2025 | Category: Laravel 9 Laravel
Generate Sitemap XML File in Laravel 9



Hi Dev,

In this article, we will explore how to generate a dynamic XML sitemap in Laravel 9. This guide provides a step-by-step explanation of how to create a custom sitemap in Laravel 9, helping search engines index your website effectively.

A sitemap is an XML file that contains the important pages of a website, allowing search engines to crawl and index them efficiently. No matter which technology you use, generating and submitting a sitemap XML file is crucial for SEO optimization.

Steps to Generate Sitemap in Laravel 9:

Step 1: Install Laravel

If you haven't already created a Laravel project, run the following command:

composer create-project laravel/laravel example-app

Step 2: Create Post Migration and Model

Generate a migration file for posts:

php artisan make:migration create_posts_table

database/migrations/create_posts_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up() {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug');
            $table->text('body');
            $table->timestamps();
        });
    }
    public function down() {
        Schema::dropIfExists('posts');
    }
};

Run the migration:

php artisan migrate

Create the Post model:

php artisan make:model Post

app/Models/Post.php

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model {
    use HasFactory;
    protected $fillable = ['title', 'slug', 'body'];
}

Step 3: Create Post Factory

Generate a factory for dummy data:

php artisan make:factory PostFactory

database/factories/PostFactory.php

<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Post;
use Illuminate\Support\Str;

class PostFactory extends Factory {
    protected $model = Post::class;
    public function definition() {
        return [
            'title' => $this->faker->text(),
            'slug' => Str::slug($this->faker->text()),
            'body' => $this->faker->paragraph()
        ];
    }
}

Generate dummy posts:

php artisan tinker
App\Models\Post::factory()->count(30)->create();

Step 4: Create Routes

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SitemapController;

Route::get('sitemap.xml', [SitemapController::class, 'index']);

Step 5: Create Sitemap Controller

Create the controller:

php artisan make:controller SitemapController

app/Http/Controllers/SitemapController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

class SitemapController extends Controller {
    public function index() {
        $posts = Post::latest()->get();
        return response()->view('sitemap', ['posts' => $posts])
            ->header('Content-Type', 'text/xml');
    }
}

Step 6: Create Blade File

resources/views/sitemap.blade.php

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    @foreach ($posts as $post)
        <url>
            <loc>{{ url('/') }}/post/{{ $post->slug }}</loc>
            <lastmod>{{ $post->created_at->tz('UTC')->toAtomString() }}</lastmod>
            <changefreq>daily</changefreq>
            <priority>0.8</priority>
        </url>
    @endforeach
</urlset>

Run Laravel App

Start the Laravel application:

php artisan serve

Access the sitemap:

http://localhost:8000/sitemap.xml

FAQ Section

What is a sitemap?

A sitemap is an XML file that lists the pages of a website, helping search engines crawl and index content more efficiently.

Why is a sitemap important?

It helps search engines discover and index important pages, improving SEO rankings.

How often should I update my sitemap?

It should be updated whenever new content is added or existing content is modified.

How can I submit my sitemap to Google?

You can submit it via Google Search Console under the "Sitemaps" section.

Does Laravel have a built-in sitemap generator?

No, but you can create one manually or use third-party packages.

Laravel 9 Sitemap Example

I hope this guide helps you!