Support the ongoing development of Laravel.io →
Article Hero Image

Automatically Generate a Sitemap for Your Website in Laravel

19 May, 2021 2 min read

Photo by Glenn Carstens-Peters on Unsplash

Hello today I have a short tutorial for you. I was working on this blog to optimize for SEO. I wanted to create a sitemap that updates automaticly. Luckly for me Spatie created a package for this. With the spatie/laravel-sitemap you can automate the process of creating a sitemap for your Laravel application. Let's learn how to setup and use this package!

I asume you have setup a Laravel application and run the migrations. Then you install the spatie/laravel-sitemap package by running the following command:

composer require spatie/laravel-sitemap

To update the sitemap frequently we need to create an artisan command. Make a GenerateSitemap.php file in the App\Console\Commands folder. Then add the following code to that file:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapGenerator;

class GenerateSitemap extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sitemap:generate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate the sitemap.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        SitemapGenerator::create(config('app.url'))
            ->writeToFile(public_path('sitemap.xml'));
    }
}

This code will generate a sitemap with the URL that you have specified in your .ENV file, and place the generated sitemap.xml file in the public folder.

That command should then be scheduled in the console kernel. To do this insert the following code in het App\Console\Kernel.php file.

protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('sitemap:generate')->daily();
    ...
}

You have to make sure that the sheduler is running on your server. I did this on my server by adding the following cronjob:

cd /var/www/vhosts/larapeak.com/github/larapeak.com && php artisan schedule:work >> /dev/null 2>&1

!IMPORTANT! The cronjob can be different on a different server. Please look at the documentation of your hosting provider.

That is it! Now you have a script that generates a sitemap each day!

Please follow me on Twitter if you want to stay in the loop and get updates when I post a new article! Have a nice day.

Last updated 1 month ago.

jinas123, joedixon, isu3ru, mckenziearts, emmadonjo liked this article

5
Like this article? Let the author know and give them a clap!
larapeak (Larapeak) https://www.larapeak.com/

Other articles you might like

Article Hero Image December 13th 2024

How to add WebAuthn Passkeys To Backpack Admin Panel

Want to make your Laravel Backpack admin panel more secure with a unique login experience for your a...

Read article
Article Hero Image December 13th 2024

Quickest way to setup PHP Environment (Laravel Herd + MySql)

Setting up a local development environment can be a time taking hassle—whether it's using Docker or...

Read article
Article Hero Image December 5th 2024

How to set up Laravel Magic Link?

User authentication is crucial for making web applications secure and easy to use. Traditionally, pa...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.