How to Generate a Sitemap with Laravel?

April 9, 2021

Create Sitemap in Laravel

You have an extensive website where you might be witnessing Google web crawlers crawling some of the recently updated pages. With its extensive archive of content pages, the site witnesses a disconnection where they are not linked to one another. You probably must be already aware of a sitemap, a file where one can list the web page.

The XML files comprise structured data of the page where you can list the web pages to convey to Google and other search engines about the site’s content. Furthermore, the web crawlers read the file for crawling the website. The developers can create a dynamic XML sitemap in Laravel.

Overview of a Sitemap

It refers to a file that can list the website pages to inform Google and other search engines about your content’s organization. The web crawlers read the file to crawl the website.

Why do you need to generate a sitemap?

That your website is a large one, it is more likely that Google web crawlers might skip crawling on some of the newly updated pages.

The site comprises an extensive archive of the content pages that are not well linked to one another. If the pages do not reference each other, these can be listed in a sitemap. This further helps Google to witness your page and not overlook it.

You might need a laravel dynamic sitemap if your site is new with a few external links. Web crawlers crawl by adhering to a few links from a page to another. This results in Google being unable to explore another page when no other websites are linked.

When your website uses enriching content, which is visible in Google News, the search engine will help you in adding a sitemap to your laravel application.

Laravel Sitemap Generator

Emerging as a robust framework as valued, websites built on Laravel are incredibly dynamic. To reflect this, one needs to update sitemap.xml regularly. With a number of existing solutions, laravel is a simple crawler based on mvdbos/PHP-spider.


This crawler, along with the generation, is triggered with the help of a Laravel CLI command. This is how it can be included in the deployment steps and to schedule an improved execution. Along with this, the Laravel Kernel command can be configured as well.

Creating a Sitemap in Laravel

If you have a Laravel app with every page being properly linked to one another, making use of three unique approaches can help you generate a sitemap. Here is how to automatically generate a sitemap with Laravel.

Single Sitemap

The function helps in generating a sitemap for a single model known as content. It would help if you had to make the controller by running the command PHP artisan make: controller SitemapCOntroller. Follow this step by installing spatie/laravel-sitemap, and run command composer require spatie/laravel-sitemap

use Spatie\Sitemap\Sitemap
public function firstSitemap( Request $request) {

    //Create Sitemap instance
    $contentSitemap = Sitemap::create();

    // Get All the published content
    $contents = Content::where(‘published’,1)->orderBy(‘created_at’ , ‘desc’)->get();

    // Loop
    Foreach ($contents as $content) }
        // put the public url of the content which is localhost/content/ {id}
        $url = url (‘content/’ .$content ->id);
        // Add the url to the sitemap

    // generate the sitemap while saving it on the desk

    // generating a success text
    $request->session()->flash(‘allert-success’, ‘sitemap generated successfully’);

    // return to the first page
    return redirect()->route(‘home’ );

In order to execute the function, you need to add it to the web routes like this while executing from there.


When the route is entered, the function propagates to generate a file in the public desk. The file is known as sitemap_content.xml, comprising the necessary content links.

Auto-generating Sitemap

This can be operated by redirecting the above-mentioned information to the public file generated. In order to manually add links, the package comprises a Sitemap Generator. The class automatically crawls the site and puts the links discovered in a sitemap.

Use Spatie\Sitemap\SitemapGenerator;

Sitemap Index

This is essential when you have multiple models where you wish to generate sitemaps for Laravel. You can then generate a sitemap index, including the varied sitemaps. In order to do this, varied sitemaps can be generated.

With a function for every sitemap, for example, first Sitemap which returns sitemap_content/XML and second Sitemap that returns sitemap_poems.xml, we can generate the sitemap index.

At first, it is mandatory to call the package file before the controller class’s onset using Spatie\Sitemap\SitemapIndex; this scenario is when the function can be written within the controller.

Key Takeaways :

Thus, as visible, developing a sitemap with Laravel is not that difficult with simple website structures. If you intend to automate the entire process, packages do exist, which might help you save some valuable time. Feel free to get in touch with LDCI, the expert Laravel Development Company India helping businesses in technology projects and to generate dynamic sitemaps in laravel for diverse technical tasks.

LDCI Written By: Kosha Shah

Leave a


Your email address will not be published. Required fields are marked *