DEV Community

Kalimah Apps
Kalimah Apps

Posted on • Updated on

Toggle maintenance mode in Laravel using web routes

As a web developer, it’s important to keep your website up to date and running smoothly. However, sometimes you need to perform maintenance on your site and you don’t want your users to see any errors or downtime. That’s where Laravel’s maintenance mode comes in handy.

Laravel’s maintenance mode allows you to put your website into a state where it is only accessible to authorized users, such as admins or developers. This allows you to perform updates or make changes to your site without disrupting your users’ experience.

Enabling maintenance mode using CLI

To enable maintenance mode, you simply need to run the following command in your terminal:



php artisan down


Enter fullscreen mode Exit fullscreen mode

This will create a maintenance mode “lock” file in your storage directory, which will prevent any users from accessing your site.

ℹ️ Note
The lock file is named "down" and it will be created in the storage/framework directory.

If you need to access your website while it’s in maintenance mode, you can use the --secret option to specify a secret endpoint that will create a browser session allowing you to bypass the maintenance mode:



php artisan down --secret=secret-route-to-allow-certain-users-to-access-the-application


Enter fullscreen mode Exit fullscreen mode

You can then access your site by appending the secret route to your site’s URL:



https://example.com/secret-route-to-allow-certain-users-to-access-the-application


Enter fullscreen mode Exit fullscreen mode

Once you’ve finished your maintenance tasks, you can simply run the following command to disable maintenance mode:



php artisan up


Enter fullscreen mode Exit fullscreen mode

This will remove the maintenance mode lock file and allow all users to access your site again.

Enabling maintenance mode using web endpoints

The above method is fine and it works well. However, it is not convenient as you would need to SSH into your server to run the commands.

To manage maintenance mode from your browser, you can add custom routes to your routes/web.php file.

To enable maintenance mode, you can add the following route:



Route::get(
    '/enable-maintenance-mode',
    function () {
        Artisan::call( 'down', [
            '--secret' => 'allow-certain-users-to-access-the-application-using-this-secret',
        ] );

        dd( Artisan::output() );
    }
);


Enter fullscreen mode Exit fullscreen mode

Notice that we are using the Artisan::call() method to run the down command. We are also passing the --secret option to specify a secret route that will create a browser session allowing you to bypass the maintenance mode.

To disable maintenance mode, you can add the following route:



Route::get(
    '/disable-maintenance-mode',
    function () {
        Artisan::call( 'up' );
        dd(Artisan::output());
    }
);


Enter fullscreen mode Exit fullscreen mode

Bounce: Custom maintenance mode page

The default maintenance mode page is not that great.

Image description

It does the job but it does not provide any extra information to the user about why the website is down, when it will up again.

If you want to display a custom maintenance mode page, you can use the --render option to specify a view that will be displayed to your users:



php artisan down --render=custom-view


Enter fullscreen mode Exit fullscreen mode

or using the Artisan::call() method:



Route::get(
    '/enable-maintenance-mode',
    function () {
        Artisan::call( 'down', [
            '--secret' => 'allow-certain-users-to-access-the-application-using-this-secret',
            '--render' => 'custom-view',
        ] );

        dd( Artisan::output() );
    }
);


Enter fullscreen mode Exit fullscreen mode

Now you can create a custom-view.blade.php file in your resources/views directory and add any content you want to display to your users.

Top comments (3)

Collapse
 
barry_allen_3f6aeec23f0b5 profile image
barry allen

So anyone going to mysite.com/enable-maintenance-mode can put my website down?

Collapse
 
smartdatasoft profile image
Muhammad Arifur Rahman

Here is a mistake

'--secrect'
will be
'--secret'

laravel.com/docs/10.x/configuratio...

Collapse
 
kalimahapps profile image
Kalimah Apps

Thanks for that. I fixed it.