DEV Community

Cover image for Laravel Maintenance Mode Example
Code And Deploy
Code And Deploy

Posted on

Laravel Maintenance Mode Example

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-maintenance-mode-example

In this post, I will show you an example of how to implement the Laravel 8 maintenance mode. If you have an update for your production server then you want to pause any transactions on your website. Laravel provides a command that will help us to do this.

Advantages of switching Laravel 8 maintenance mode

It will help us to surely not allow save, update, delete and retrieve records from DB during the maintenance mode. It will help us also that our users are not able to access the application.

To perform maintenance mode on Laravel, run the down Artisan command.

# enable maintenance mode
php artisan down

# disable maintenance mode
php artisan up

# if you want visitor refresh the page after number of seconds being set
php artisan down --retry=60
Enter fullscreen mode Exit fullscreen mode

Bypassing Laravel Maintenance Mode

If you want to bypass the maintenance mode and test the updates of your Laravel application we can use the secret option and bypass using provided token. See the below example:

php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"
Enter fullscreen mode Exit fullscreen mode

We can also use the browser by putting the secret key with your URL. See the below example:

https://you-site.com/1630542a-246b-4b66-afa1-dd72a4c43515
Enter fullscreen mode Exit fullscreen mode

Take note that when accessing this hidden route, you will then be redirected to the / route of the application. Once the cookie has been issued to your browser, you will be able to browse the application normally.

Custom View for Laravel Maintenance Mode

If you want to render a specific template or blade for your Laravel maintenance mode you can add the option --render="your file name" from your views. See the below example:

# view path: resources/views/maintenance.blade.php
php artisan down --render="maintenance"
Enter fullscreen mode Exit fullscreen mode

Redirect All Request to Home Page in Laravel Maintenance Mode

If you need to restrict your visitors when Laravel maintenance mode is enabled to your web pages you can redirect them to your home page. The example below will help how to do that:

php artisan down --redirect=/
Enter fullscreen mode Exit fullscreen mode

Additional Note: During the maintenance mode all your job queues will pause also.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-maintenance-mode-example if you want to download this code.

Happy coding :)

Top comments (1)

Collapse
 
valdir_lima_d913174715c68 profile image
Valdir Lima • Edited

Excellent