How to generate sitemap in laravel 11 |
How to generate sitemap in laravel 11 |

How to generate sitemap in laravel 11

In the digital realm, having a sitemap for your website is crucial. It’s like a roadmap for search engines, helping them navigate through your site efficiently. Fortunately, Laravel, one of the most popular PHP frameworks, simplifies the process of generating a sitemap. In this tutorial, we’ll walk through the steps to create a sitemap in Laravel 11.

Step 1: Installation

Before diving into the code, ensure you have Laravel 11 installed on your system. If not, you can install it using Composer by running the following command in your terminal:

composer create-project laravel/laravel my-project "11.*"

Once Laravel is installed, navigate to your project directory using the terminal.

Step 2: Install the Laravel Sitemap Package

Laravel offers various packages to extend its functionality. We’ll use the “spatie/laravel-sitemap” package to generate our sitemap. Install it via Composer:

composer require spatie/laravel-sitemap

After installation, Laravel will automatically discover the package.

Step 3: Create Sitemap Routes

In Laravel, routes define the entry points to your application. To generate a sitemap, we need to define routes that correspond to the pages we want to include in our sitemap. Open the routes/web.php file and add the following routes:

use Spatie\Sitemap\SitemapGenerator;

Route::get('/sitemap.xml', function () {
return response()->view('sitemap');

Replace '' with your actual website URL.

Step 4: Create Sitemap View

Now, let’s create a view for our sitemap. In the resources/views directory, create a file named sitemap.blade.php and add the following content:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="">
<loc>{{ url('/sitemap.xml') }}</loc>
<lastmod>{{ \Carbon\Carbon::now()->format('Y-m-d') }}</lastmod>

This view will generate the XML structure for our sitemap.

Step 5: Generate the Sitemap

Now, it’s time to generate our sitemap. Simply access the route in your browser, and Laravel will dynamically generate the sitemap XML file based on the routes defined earlier.

Step 6: Test Your Sitemap

To ensure everything is working correctly, open the generated sitemap.xml file in your browser. You should see a well-formed XML document containing the URLs of your website pages.

Step 7: Automate Sitemap Generation (Optional)

You may want to automate the sitemap generation process to keep it up-to-date as your site evolves. One way to achieve this is by scheduling a command to generate the sitemap periodically. Add the following command to the App\Console\Kernel.php file:

use Spatie\Sitemap\SitemapGenerator;

protected function schedule(Schedule $schedule)
$schedule->call(function () {

Replace '' with your actual website URL.


Generating a sitemap in Laravel 11 is straightforward, thanks to the spatie/laravel-sitemap package. By following the steps outlined in this tutorial, you can ensure that your website is properly indexed by search engines, leading to improved visibility and accessibility. Happy coding!