YouTube Icon

Code Playground.

How to Create Sitemap XML Using Laravel

CFG

How to Create Sitemap XML Using Laravel

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.




CFG