Adding a sitemap to your laravel application is a very important feature in order for your website to communicate with search engines. It's a way to tell those search engines about all your content and boost up your SEO.

In this article, we will use one of the most common packages to generate sitemaps for Laravel, spatie/laravel-sitemap.

We will learn how to generate sitemaps in three different approaches.

  1. Single Sitemap
  2. Auto-Generating Sitemap
  3. Sitemap index

Let's make our controller by running this command php artisan make:controller SitemapController. Then install spatie/laravel-sitemap by running this command composer require spatie/laravel-sitemap.

Now let's dive in to the process!

Single Sitemap

This function will generate a sitemap for a single model named Content. Let's go to our previously made controller SitemapController and add this line before the opening of the class name

use Spatie\Sitemap\Sitemap;

Then, Let's write this function inside the controller class, Each line is explained with the comment above it.

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) {
            // Write the public url of this content which is in our case localhost/content/{id}
            $url = url('content/'.$content->id);
            // Add the url to the sitemap
            $contentSitemap->add($url);
        }
        // generate the sitemap and save it on the desk
        $contentSitemap->writeToDisk('public', 'sitemap_content.xml');
				// generating a success message
				$request->session()->flash('alert-success', 'sitemap generated successfully');
				// return to the first page
				return redirect()->route('home');
    }

Now to execute this function we will add it to our web routes like this and excute it from there

Route::get('/first_sitemap', [SitemapController::class, 'firstSitemap'])->name('sitemap.first');

when we enter this route the function will generate a file in the public desk called sitemap_content.xml which contains all the content links.

Auto-Generating Sitemap

Sadly, this package doesn't provide a native way to auto-generate sitemaps (or maybe I didn't find that feature). But we can make this happen by redirecting the above function to the public file that has just been generated by it. I don't recommend that for big sitemaps but anyway, let's write the function

public function firstSitemap() {
        // 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) {
            // Write the public url of this content which is in our case localhost/content/{id}
            $url = url('content/'.$content->id);
            // Add the url to the sitemap
            $contentSitemap->add($url);
        }
        // generate the sitemap and save it on the desk
        $contentSitemap->writeToDisk('public', 'sitemap_content.xml');
        // return the above generated sitemap file
        return \File::get(public_path() . '/sitemap_content.xml');
    }

Sitemap index

sitemap index is important if you have multiple models and you want to generate a sitemap for each model. then you can generate a sitemap index that includes all the other sitemaps.

to do that, we can generate multiple sitemaps first as we did in the first section above. One function for each sitemap, for example firstSitemap that returns sitemap_content.xml and secondSitemap which returns sitemap_poems.xml. then, we can generate our sitemap index like this

First, we need to call this package file before the opening of the controller class use Spatie\Sitemap\SitemapIndex;. After that we can write this function inside the controller

public function sitemap() {
        // Create a sitemap index instance
        $sitemapIndex = SitemapIndex::create()
        // Add the sitemaps by writing there path in the public folder
        ->add('/sitemap_content.xml')
        ->add('/sitemap_poems.xml');
				// generate the sitemap index and save it on the desk
        $sitemapIndex->writeToDisk('public', 'sitemap_index.xml');
				// generating a success message
				$request->session()->flash('alert-success', 'sitemap index generated successfully');
				// return to the first page
				return redirect()->route('home');
    }

And that is pretty much it. I hope you learned something new today and thanks for reading!

Comments