Maintenance mode for Laravel application

Today I am going to show you one artisan command which is a very helpful command. Well, I am talking about the Artisan::call(‘up’) or Artisan::call(‘down’) command. Using this command you can build your own maintenance mode functionality.

This command will help you when something went wrong to your site and you need to shut down your site for the little times for the troubleshooting.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

If you just want to test this feature then open your Laravel project in the terminal and run php artisan down and then open your project in the browser and see the reflection on the site.

maintenance mode

Recently I have integrated maintenance mode functionality in my Laravel project and I must tell you that it’s too easy to set up on any Laravel project without any troubles.

Let’s say I have two types of user roles:

  1. Super Admin
  2. Business Owner

And I want to access login route & super admin group routes even If maintenance mode is enabled just like WordPress.

maintenance mode

Now let’s jump to the code and follow the steps.

Step 1:

Create new custom middleware.

php artisan make:middleware CheckForMaintenanceMode

Step 2:

After creating middleware open CheckForMaintenanceMode.php file from app/Http/Middleware directory and replace it with below code.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;

class CheckForMaintenanceMode
{
    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function handle($request, Closure $next)
    {
        if ($this->app->isDownForMaintenance() && !$this->isBackendRequest($request)) {
            $data = json_decode(file_get_contents($this->app->storagePath() . '/framework/down'), true);

            throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']);
        }

        return $next($request);
    }

    private function isBackendRequest($request)
    {
        return ($request->is('login') or $request->is('admin/*') or $request->is('api/*'));
    }
}

Inside isBackendRequest function, you can define routes that you want to allow even If maintenance mode is enabled.

Step 3:

Now we have to include CheckForMaintenanceMode middleware inside Kerel.php file and for that navigate to app/Http directory.

Here you have to do two things:

1. Add below one line of code inside protected $middleware = [ … ]

'App\Http\Middleware\CheckForMaintenanceMode',

2. Remove CheckForMaintenanceMode::class code from protected $middleware = [ … ]

\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

Step 4:

To make your site Online/Offline, create a new form with drop-down selection(Online/Offline) somewhere in your admin panel and manage flag into the database (is_online 1/0). Whenever the admin user changes the site availability, run Artisan code based on the selection.

if ($request->get('is_online') == '1') {
    \Artisan::call('up');
} else {
    \Artisan::call('down');
}

That’s it. You have successfully created a maintenance mode functionality in your Laravel application.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Author: Manan Patel

Freelancer + Passionate web developer focused on backend and frontend development.

One thought on “Maintenance mode for Laravel application”

Leave a Reply