Question

How to Automatically Generate Sitemap with Laravel?

Posted October 8, 2019 14.3k views
Linux BasicsPHP Frameworks

Hi all,

I’m almost done with building a small website using Laravel. I’m now working on the SEO side of things and I want to be able to automatically generate the sitemap.xml file on let’s say daily basis.

Has anyone done this before and is there a package that you could suggest? Or is it better for me to create the logic myself?

Thanks!

1 comment
  • use Spatie\Sitemap\Sitemap; use Spatie\Tags\Url; $sitemap = Sitemap::create() ->add(Url::create(’/home’)) ->add(Url::create(’/contact’)); NewsItem::all()->each(function (NewsItem $newsItem) use ($sitemap) { $sitemap->add(Url::create(“/news/{$newsItem->slug}”)); }); Projects::all()->each(function (Project $project) use …

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
3 answers

Hello,

I usually use the following package for all of my sites to generate the sitemap.xml file automatically:

https://github.com/spatie/laravel-sitemap

The setup goes something like this:

  • First install the package via composer:

composer require spatie/laravel-sitemap

  • Configuration

If you would like to change the default options, you could publish the config by using the following command:

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

This will copy the config to config/sitemap.php where you would be able to change it depending on your needs.

  • Usage

To generate a sitemap for your website with all of the found links, you can use the following:

SitemapGenerator::create('https://example.com')->writeToFile(public/sitemap.xml);
  • Automation

To automate the process what you could do is create an artisan command and then add it to your scheduler.

To do that just run the following:

php artisan make:command GenerateSitemap

And then update the content of the app/Console/Commands/GenerateSitemap.php file accordingly. I would usually use something like this:

<?php

namespace App\Console\Commands;

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

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // modify this to your own needs
        SitemapGenerator::create(config('app.url'))
            ->writeToFile(public_path('sitemap.xml'));
    }
}

Then to generate your sitemap just run:

php artisan sitemap:generate

To schedule this to run on daily basis just add the following to your app/Console/Kernel.php file:

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

That is pretty much it, then your sitemap.xml should be available at example.xml/sitemap.xml.

For more information I would recommend going through the official repo here:

https://github.com/spatie/laravel-sitemap/blob/master/README.md

Hope that this helps!
Regards,
Bobby

Many thanks for your swift response. I believe, at least in my case, the line that is in the crontab has another folder, html, to get to my domain folder.
***** cd /var/www/html/app_folder && php artisan schedule:run >> /dev/null 2>&1

Thank you
David

Thank you for the above. Very helpful. Wondering if anyone would have a good tutorial on setting up the cron to reference the kernel to run this.

Thank you

  • Hi @dukesnuz,

    Yes, so what you would need to do is the following:

    • SSH to your Droplet

    • Then edit your Crontab:

    crontab -e
    
    • And at the bottom add the following
    * * * * * cd /var/www/your_domain.com && php artisan schedule:run >> /dev/null 2>&1
    

    That is pretty much it, then the cron would run every minute and execute any scheduled tasks as defined in your app/Console/Kernel.php.

    Hope that this helps!
    Regards,
    Bobby

Submit an Answer