Use Laravel's Maintenance Mode in Statamic

16th March 2020

Now that Statamic is just a package inside a Laravel application, it means you can take advantage of Laravel features.

There's a Laravel feature called Maintenance Mode which means visitors will see a 'site down' message. In this article, I'm going to show you how to use it and how to setup your own custom site down screen.

First, let's understand how it works. Laravel's command line, artisan, comes with two commands. php artisan down and php artisan up. The down command takes the site down and the up command puts the site back up. Simple.

When you take a Laravel app down, it stops users from accessing any pages on the site. However, you may want to override this for a few routes, for example, like the Control Panel.

It's actually really easy to do this. Just open up App\Http\Middleware\CheckForMaintenanceMode.php. It should look like this:

php
namespace App\Http\Middleware;
 
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
 
class CheckForMaintenanceMode extends Middleware
{
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array
*/
protected $except = [
//
];
}

To add the Control Panel to our exceptions list, just do something like this:

php
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array
*/
protected $except = [
'/cp/*',
];

Now you should be able to access the Control Panel while your Laravel app is in maintenance mode.

By default, your visitors will see a screen like this while you maintenance mode turned on.

However, it's likely that you'll want to customise this page to fit in with the site. It's actually really easy to do. Just create a Blade or Antlers view with this filename. resources/views/errors/503.blade.php

Whatever you put in that file will get picked up and used instead of the one Laravel provides.

And that's it - it's really that simple!