Last updated on February 17, 2018
Enabling and disabling maintenance mode in Laravel 5 is simple, you just need to issue following artisan commands –
- To enable maintenance mode
php artisan down
. - To disable maintenance mode
php artisan up
.
That is it. If you want to allow the application to listed IPs, even when the application is in maintenance mode. We need to override the default middleware : \vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php (default laravel5 uses).
First we need to get current user ip address by using laravel5 powerful request object method $this->request->getClientIp()
Than copy the default laravel5 maintenance middleware in \app\Http\Middleware\CheckForMaintenanceMode.php and make changes as shown below –
app = $app; $this->request = $request; } public function handle($request, Closure $next) { if ($this->app->isDownForMaintenance() && !in_array($this->request->getClientIp(), ['127.0.0.1','202.65.140.116'])) { throw new HttpException(503); } return $next($request); } }
Now open \app\Http\Kernel.php file and comment out default middleware and add our custom middleware
'App\Http\Middleware\Authenticate', 'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth', 'guest' => 'App\Http\Middleware\RedirectIfAuthenticated' ]; }
Maintenance Mode Response Template – The default template for maintenance mode responses is located in resources/views/errors/503.blade.php
.