loading...

Simple Laravel Sitemap Generator

spekulatius1984 profile image Peter Thaleikis 🍪 Originally published at bringyourownideas.com ・2 min read

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 "noindex" as well as canonical URLs by default. The last modified date 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.

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 generate:sitemap

This will fetch the required resources, analyze 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.

More Details

More information can be found on the GitHub repository for the project: https://github.com/bringyourownideas/laravel-sitemap.

Posted on by:

spekulatius1984 profile

Peter Thaleikis 🍪

@spekulatius1984

Developer and indie maker. Building little projects in the hope someone likes them :D

Discussion

pic
Editor guide