How to Exclude Routes from Maintenance Mode in Laravel Using PreventRequestsDuringMaintenance

To exclude specific routes from maintenance mode in Laravel, you can use the PreventRequestsDuringMaintenance middleware. This middleware allows you to specify certain URIs that should remain accessible even when the application is under maintenance.

Here's how to configure the middleware to exclude specific routes from maintenance mode:

Locate the Middleware: The PreventRequestsDuringMaintenance middleware is usually located at app/Http/Middleware/PreventRequestsDuringMaintenance.php.

Define URIs to Exclude: In this middleware file, you will find an $except property. This property is an array that lists all the URIs that should be reachable even when the application is in maintenance mode. Update this property to include the routes you want to exclude.

For example, the middleware might look like this:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Middleware;

class PreventRequestsDuringMaintenance extends Middleware
{
    /**
     * The URIs that should be reachable while maintenance mode is enabled.
     *
     * @var array<int, string>
     */
    protected $except = [
        '/api/*',      // Exclude all routes under /api
        '/cms/preview' // Exclude specific route /cms/preview
    ];
}

In this example:

  • All routes under /api are excluded from maintenance mode (/api/*).
  • A specific route /cms/preview is also excluded.

Deploy Changes: Save the changes to the middleware file and deploy your application. The specified routes will now be accessible, even when the application is in maintenance mode.

Understanding Maintenance Mode in Laravel

Maintenance mode is a feature in Laravel that allows you to "pause" your application, displaying a custom maintenance page to users. This is particularly useful when performing updates or critical maintenance tasks on your application.

When your application is in maintenance mode, Laravel intercepts all incoming requests and displays a 503 "Service Unavailable" status code, along with a customizable message to users, preventing them from accessing the application. However, using the PreventRequestsDuringMaintenance middleware, you can specify certain routes to remain accessible.

How to Enable and Disable Maintenance Mode

To enable maintenance mode in Laravel, use the following Artisan command:

php artisan down

This command puts the application into maintenance mode, displaying a generic maintenance page to users.

To disable maintenance mode and bring your application back online, use:

php artisan up

Using Secrets to Bypass Maintenance Mode

Laravel allows you to set a "secret" to bypass maintenance mode, which is particularly useful for developers or administrators who need to access the application without disabling maintenance mode for all users.

To enable maintenance mode with a secret, run:

php artisan down --secret="your-secret-key"

Once set, you can access the application by appending the secret to your application's URL:

http://your-app-url.com/your-secret-key

Conclusion

Excluding specific routes from maintenance mode using the PreventRequestsDuringMaintenance middleware in Laravel is an efficient way to ensure that critical parts of your application remain accessible during updates or maintenance. By simply adding URIs to the $except property in the middleware, you can keep certain routes operational, providing flexibility and control over your application's availability.