Laravel Sitemap Package

Introduction

This is a package to make it easier to generate and maintain multilingual XML sitemaps for Google. Once I figured out the convoluted requirements for hreflang tags and return tags I quickly realized it would be a nightmare to manually maintain multilingual sitemaps so I wrote this package to automate it.


Installation

composer require escuccim/sitemap

Then register the components in /config/app.php:

Add the following to the 'providers' array:

Escuccim\Sitemap\sitemapServiceProvider::class,

And add the following to 'aliases' array:

Run the database migrations to create the blog tables and update the users table:

php artisan migrate

By default I use a middleware I call 'admin' to verify if the user has permission to access the administrative pages. If you wish to use this you need to register the middleware in /app/Http/Kernel.php in $routeMiddleware:

'admin' => \Escuccim\Sitemap\Middleware\AdminMiddleware::class,

If you wish to use your own middleware or stick with Laravel's Auth middleware you can do so by updating the config (see below).


Usage

This package contains its own routes, models, controllers and views so should work after installation. To access it just go to /sitemapadmin. By default it will generate two sitemaps, one is a sitemap index available at /sitemap, the other is a sitemap for the static pages available at /sitemap/pages.

If you wish to edit my views or other files you can publish them to your application:

php artisan vendor:publish

This will publish all of the files. To only publish certain files add --tag=[group], using the groups listed below:

  • config - publishes the config file to /config/sitemap.php
  • views - publishes the views to /resources/views/vendor/escuccim

The administrative page at /sitemapadmin will have three sections:

  • Static Page Admin - where you can add and edit the static pages you want included in your sitemap
  • Sitemap Admin - where you can list additional sitemaps you want listed in your sitemap index.
  • Subdomain Admin - where you can specify the subdomains you want referenced in your sitemap with their corresponding languages.

Static Page Admin

Should be fairly self-explanatory. To associate images with the page add them in the edit screen for the appropriate page. All of the values associated with each page are used in the sitemap except 'name'.

Sitemap Admin

Allows you to list additional sitemaps in your sitemap index. For example I have an additional sitemap for blog articles. Note you will need to create these sitemaps on your own, but it should be relatively easy if you copy the view file for pages and change the PHP variables accordingly.

Each sitemap has two fields you can edit:

  • URI - is the URI to the sitemap
  • table - allows you to specify a DB table for the sitemap. If you specify one the lastmod date in the sitemap will be pulled from the latest updated_at date in the table specified here.

Subdomain Admin

This is where you list your subdomains and their associated languages. Each subdomain has two fields in the edit and add screens:

  • Subdomain - the subdomain you wish to have appear in the sitemap (ie. www). You should also include a subdomain of blank if your site does not require a subdomain
  • Language - the language code for the language associated with the subdomain.

From the main page you can also specify a default subdomain which will be listed in the sitemap as the x-default.

The sitemap generated will have an entry for each page and each subdomain, along with hreflang tags again for each page and each subdomain.


Configuration

To access the configuration files you need to publish the config file to /config/sitemap.php with

php artisan vendor:publish --tag=config.

The config file has the following values:

  • href_lang_per_subdomain - this specifies if you want the sitemap to list the subdomains with their hreflang tags.
  • middleware - the middleware used to determine if the user has permission to access administrative pages. Defaults to my admin middleware, if you want to use Laravel's auth replaced this with 'auth' or you can use your own middleware.