Laravel 10 Generate Sitemap XML File Tutorial |
Laravel 10 Generate Sitemap XML File Tutorial |

Laravel 10 Generate Sitemap XML File Tutorial


Sitemaps play a crucial role in search engine optimization (SEO) by helping search engines index your website’s content effectively. In this tutorial, we will explore how to generate a sitemap XML file in a Laravel 8 application. We’ll use the Laravel Sitemap package, which simplifies the process of creating and managing sitemaps.


Before getting started, ensure that you have the following prerequisites:

  1. Laravel 8 Project: Set up a Laravel 8 project if you haven’t already.
  2. Composer: Install Composer, a dependency manager for PHP, as we’ll use it to install packages.

Step 1: Install Laravel Sitemap Package

Open your terminal and navigate to your Laravel project. Run the following command to install the Laravel Sitemap package:

composer require spatie/laravel-sitemap

This command will download and install the package in your Laravel project.

Step 2: Configure Laravel Sitemap

After installing the package, you need to publish its configuration file. Run the following command:

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

This command will create a sitemap.php file in the config directory of your Laravel project.

Step 3: Create a Sitemap Controller

Next, let’s create a controller that will be responsible for generating the sitemap. Run the following command:

php artisan make:controller SitemapController

This will create a SitemapController.php file in the app/Http/Controllers directory.

Step 4: Define Routes in the Sitemap Controller

Open the SitemapController.php file and define the routes for which you want to generate URLs in the sitemap. For example:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Sitemap\SitemapGenerator;

class SitemapController extends Controller
    public function generate()

        return response('Sitemap generated successfully.');

In this example, the generate method uses the Laravel Sitemap package to generate a sitemap XML file and save it to the public directory with the name sitemap.xml.

Step 5: Define a Route for Sitemap Generation

Open the web.php file in the routes directory and define a route that maps to the generate method of the SitemapController:

use App\Http\Controllers\SitemapController;

Route::get('/sitemap', [SitemapController::class, 'generate']);

This route will be used to trigger the generation of the sitemap.

Step 6: Run Your Laravel Application

Finally, run your Laravel application using the following command:

php artisan serve

Visit in your browser, and the sitemap XML file will be generated and saved in the public directory.

Congratulations! You have successfully created a sitemap XML file for your Laravel 8 application. This sitemap can now be submitted to search engines to improve the indexing of your website’s content.


Generating a sitemap is an essential step in optimizing your website for search engines. With the Laravel Sitemap package, the process becomes straightforward and manageable. You can customize the sitemap generation further based on your specific requirements.

Leave a Reply