Step 1: Install Laravel Sitemap by Spatie
Firstly do Install the package using Composer package manager to your Laravel project.
composer require spatie/laravel-sitemap
Step 2: Generate Sitemap
<?php Route::get('generate-sitemap', function () { SitemapGenerator::create('https://app.test') ->writeToFile($path); });
<?php Route::get('generate-sitemap', function () { SitemapGenerator::create('https://app.test') ->writeToFile(public_path('sitemap.xml')); });
https://app.test/sitemap.xml
Step 3: Update robots.txt
Before pushing your code to production do make sure to update the "robots.txt" to specify the location of the sitemap.
User-agent: * Disallow: Sitemap: https://app.test/sitemap.xml
Optional: Sitemap Configuration File
You may publish the sitemap configuration file and from the config, you can specify how long the timeout of the crawler request and etc.
php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=config
config/sitemap.php
Leave a reply