Sitemap helps to enhance the url crawl for a website. It lists all the links at a single place. It is a method to rank urls to search engines. Inside this article we will see the concept of Laravel 8 sitemap generator package.
How to generate automatic sitemap for website, we will see in a very easy and detailed way. We will list all urls into XML format. We will use a composer package for this task.
Generating sitemap xml format in a dynamic way in laravel 8.
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
To start the development server of Laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Installation of Spatie Laravel Package
Open project into terminal and run this composer command.
$ composer require spatie/laravel-sitemap
Package Publish & Configuration
$ php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=config
Usage of Spatie Sitemap Package
Syntax
SitemapGenerator::create('SITE-URL')->writeToFile("FILENAME-TO-WRITE");
Example
We will create a closure route in web.php. Open web.php from /routes folder.
<?php use Illuminate\Support\Facades\Route; use Spatie\Sitemap\SitemapGenerator; Route::get("generate-sitemap", function () { SitemapGenerator::create('https://onlinewebtutorblog.com/')->writeToFile(public_path('sitemap.xml')); dd("done"); });
- When we run this, it will create a file with the name sitemap.xml file inside /public folder which contains the urls of given website.
- To generate sitemap.xml file for itself, we need to pass app url, Use config(‘app.url’)
SitemapGenerator::create(config('app.url'))->writeToFile(public_path('sitemap.xml'));
If we open sitemp.xml file, we will see the list of urls. In this case, it will be something –
URL – http://127.0.0.1:8001/sitemap.xml
But every time, we need to run the route manually like http://127.0.0.1:8000/generate-sitemap to generate or update sitemap.xml file.
To automate this process, we need to use laravel 8 command method. We have to create scheduled cron job which runs this same url but at a specific time period.
Create Artisan Command
Open terminal and run this artisan command.
$ php artisan make:command GenerateSitemap
It will create a file GenerateSitemap.php at /app/Console/Commands folder.
Open GenerateSitemap.php and write this complete code into it.
<?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 = 'This command will generate sitemap.xml file for your site'; /** * 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')); } }
Open terminal and run this command
$ php artisan
This command will open the command pallet of laravel artisan, we should see now the created custom command.
Run Artisan Command
To run this created artisan command. Back to terminal
$ php artisan sitemap:generate
It will create sitemap.xml to your /public folder.
Schedule Laravel Command
To automate this process, we need to set a specific time interval to run. Open Kernel.php from /app/Console folder.
Find schedule() method from that and add this given line into it.
/** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->command('inspire')->hourly(); $schedule->command('sitemap:generate')->daily(); }
This is very simple, then your sitemap.xml should be available at site_url/sitemap.xml
We hope this article helped you to learn about Laravel 8 Sitemap Generator Package Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.