Automatically Generate Sitemap in Laravel

How to Automatically Generate Sitemap with Laravel?


As we want to integrate the url or links into our sitemap.xml file, so Laravel provide us a package called Spatie Sitemap Package. So, Now We are going to install the Spatie-Laravel-Sitemap Package to generate all the link automatically.

Step 1: First, install the package via composer by following commad:

$ composer require spatie/laravel-sitemap

$ composer require spatie/laravel-sitemap "5.8"

Step 2: Configuration: You can override the default options for the crawler. and then publish the configuration by using the following command :

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

Step 3: Thye above command line will copy the config to config/sitemap.php where you would be able to change it depending on your needs.

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

SitemapGenerator::create(config('app.url'))->writeToFile(public_path('path/sitemap.xml'));
To use this above statement and where to paste this statement, follow with the below steps.

Step 5: To automate the process, you have to create an artisan command by the following: 

$ php artisan make:command GenerateSitemap
after running the above artisan command a file has been generated in app/Console/Commands/GenerateSitemap.php file. and the lets update the inside content as I have done below :
<?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 mixed
     */
    public function handle()
    {
        SitemapGenerator::create(config('app.url'))->writeToFile(public_path('path/sitemap.xml'));
    }
}

Step 6: Now we have successfully setup for generating the Sitemap code and let's run the artisan command by following:

$ php artisan sitemap:generate
After running the artisan command successfully, you the able to your all your url or links in your sitemap.xml file where you assigned the path.

Note: If you want to run this on server eg: gogaddy or namecheap server, Goto your filemanager and start your teminal and check composer is installed. and the run the above artisan command to generate the sitemap.xml and its url orelse paste the below command to update automatically. (If You want right now to check the Url in sitemap.xml file - follow Step 8)

Step 7: If you want to update or add the url on Daily basis, paste the below command in the following path app/Console/Kernel.php file::
protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('sitemap:generate')->daily();
    ...
}

Step 8: If you want to RIGHT NOW to check and execute the output on your server and check the Links are generated or not in the sitemap.xml file, then you can use Artisan Console Command by following.

Route::get('/generate-sitemap'function() {
    $exitCode = Artisan::call('sitemap:generate');
    return 'Sitemap Created Successfully'//Return anything
});

 
Step 9: Finally, you can see your sitemap in your given path: eg: domainname.com/yourpath/sitemap.xml

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