Generate A Laravel Sitemap Dynamically Complete Tutorial - FreeCodeBlog

Last updated: Jun 30, 2022

Want to generate a laravel sitemap dynamically? Actually yes I want to create a system that will generate a sitemap in laravel dynamically. Well it's a very good idea to make a generating laravel sitemap. Want to add website URL in laravel sitemap dynamically? Yes, I really want to add URLs in laravel sitemap when laravel sitemap will generate dynamically. Want to create or generate sitemap in laravel 7, laravel 6, laravel 8 and all available laravel version? Yeah. Okay don't worry you can easily make a laravel sitemap dynamically that will generate a xml file in laravel whenever you want. So let's start to read laravel dynamic sitemap tutorial.


Generate A Laravel Sitemap Dynamically Complete Tutorial
Generate A Laravel Sitemap Dynamically Complete Tutorial


1. Install Spatie Laravel Sitemap.

I will install spaite laravel sitemap to create a dynamic sitemap in laravel. You may use others sitemap package to generate a sitemap in laravel dynamically. You can easily install spaite laravel sitemap package via composer.


composer require spatie/laravel-sitemap


2. Configure Spatie Laravel Sitemap.

I hope your laravel project have a installed spaite laravel sitemap package. After installed sitemap package in laravel, You need to configuration laravel sitemap spaite package. Want to copy default config sitemap file? Okay this will copy the default config to config/sitemap.php where you can edit it.


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


3. Config Sitemap PHP file.

Let's see config sitemap php file that you did publish the spaite default config to config/sitemap.php. You will see some guzzle options in sitemap PHP file. You will see COOKIES, CONNECT_TIMEOUT, TIMEOUT, ALLOW_REDIRECTS into guzzle options. Next you will also see execute_javascript, chrome_binary_path and crawl_profile options in sitemap php file.


4. Sitemap Generator Command.

Really want to generate laravel sitemap dynamically? Yes. Then you must need to use laravel command to generate dynamic laravel sitemap. Now you need to make a command in laravel to generate a dynamic laravel sitemap. I hope you have a basic knowledge of laravel command. Okay you will easily make a command for laravel sitemap using artisan command. You will see the generated Laravel command in the App/Console/Commands folder.


php artisan make:command SitemapGenerator


5. Open Sitemap Generator Command.

Have you created sitemap generator command? Yes. Well you need you open sitemap generator command from commands folder to configure spaite laravel sitemap.




6. Add Sitemap Generator Command Signature.

Did you open the Sitemap Generator command? Yes. Good, Now you need to add a signature in generated sitemap command. This signature will generate a sitemap xml file in public folder when you will run sitemap:generate command.


protected $signature = 'sitemap:generate';


7. Add Sitemap Generator Command Description.

You can add a laravel sitemap generated related a good description in SitemapGenerator.php file. This description will not effect to generate laravel sitemap file. So it's doesn't matter that you did add description in command file or not.


protected $description = 'Generate A Laravel Sitemap Dynamically Complete Tutorial.';


8. Use Spaite Sitemap In Command.

It's time to use spaite sitemap in SitemapGenerator.php file. You need to use spaite sitemap and sitemap tag URL in sitemap generator command file. You can easily generate website URLs in sitemap xml file using this two file.


use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;


9. Use Carbon Time In Command.

Want to use last modification date in sitemap xml file? Yes. Then you need to use carbon time. You can also use database date time as a last modification date.


use Carbon\Carbon;


10. Use Spaite Sitemap In Command.

Finally you need to create a sitemap xml file in laravel dynamically. I hope you did all other configuration before generated a sitemap in laravel dynamically.


$sitemap = Sitemap::create();
$sitemap->writeToFile(public_path() . sitemap.xml);


11. Generate Sitemap Xml File.

I hope you did all configuration so that you can easily generate a sitemap XML file in public folder. Now it's time to generate a xml file in public folder in laravel dynamically.


php artisan sitemap:generate


Okay so this is for today. See you again next tutorials. Thanks

Post Tags
Post Writer
Masum Biswas (@masum7323)

My name is Masum Biswas. I'm a full stack developer. I live in Bangladesh and I love to write tutorials and tools that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, MySQL, VueJS, Tailwind CSS and Bootstrap from the early stage.

Follow Me On
Related Posts
How To Create Singular To Plural Converter Online Tool In Laravel

Create singular to plural converter online tool in laravel to convert singular word "car, student, teacher" to plural word "cars, students, teacher... Read More

How To Configure Laravel Project For Production

Want to deploy laravel project on live server, shared hosting, vps server, production? Wait a minute, have you configured the Laravel project for p... Read More

Make A Laravel Pdf Generator Using Barryvdh Dompdf

Make a laravel pdf generator in laravel using barryvdh dompdf. You can easily generate a pdf file from a HTML file using DomPDF. To export into PDF... Read More

Develop A Simple Free Online Web Calculator Application In Laravel

We can easily develop a simple free online web calculator for quick calculations in laravel that support button click. So welcome to one of the bes... Read More

Best Web Hosting For Blog Website

Web hosting means giving storage space and access for websites. Things are a lot easier in digital life than physical life. In Digital world you do... Read More