Simple Laravel Sitemap Generator
Laravel is a powerful framework we all value. Websites built on Laravel are often dynamic. To reflect this the sitemap.xml needs to be kept up to date regularly. While there are existing solutions to do this it's been a pain at scale - headless browsers are used to suit JavaScript-heavy websites. Websites without JavaScript frontend aren't in need of this and it's a resource-intense overkill. We have opted to use a simple crawler instead. It's based on mvdbos/php-spider.
Workings
The crawler considers the robots attribute "noindex" as well as canonical URLs by default. No further configuration is needed to make it behave. The last modified date in the sitemap is based on the "article:modified_time" value, or alternatively, the current date. Priorities are guessed based on the depth of the page in the website.
The crawl and generation are triggered using a Laravel CLI command. This way, you can include it in your deployment steps and/or schedule regular execution. Alterrnatively, you can configure a Laravel Kernel command (more below).
Installation
You can install the package using composer:
composer require bringyourownideas/laravel-sitemap
Further details can be found on the repository. With this step the required service provider is automatically installed. If you have opted out from auto discovery you will need to install the Laravel service provider manually:
php artisan vendor:publish --provider="BringYourOwnIdeas\LaravelSitemap\SitemapServiceProvider"
Usage
To trigger the crawl and generate of the sitemap you need to run the following artisan command:
php artisan sitemap:generate
This will fetch the required resources, analyse the web-pages and write the sitemap.xml file into the "public"-directory. To regularly update it you can schedule the execution in your "app/Console/Kernel.php"-file.
Automated Updating of the sitemap.xml in Laravel
To trigger regular updates of the site you can add a trigger of the Artisan command to your Laravel console kernel. As mentioned, the file is located under "app/Console/Kernel.php". Update the "schedule"-method to enable regular updates:
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// generate the sitemap
$schedule->command('sitemap:generate')->daily()->at('00:42');
}
The configuration added above will re-generate and publish the sitemap every night at 0:42.
More Details
More information can be found on the GitHub repository for the project: https://github.com/bringyourownideas/laravel-sitemap.