Setting up a maintenance mode in laravel 5

In this post i will show you how to enable maintenance mode in laravel 5 framework. Sometimes you need to down your website so you should enable the maintenance mode for reflecting developement goes on.

Laravel Middleware


Enable the maintenance mode: 
php artisan down

Disable the maintenance mode: 
php artisan up

In this if you want to allow some IP's then we need to override the \vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenace Middleware.

Step 1: You can get the ip address by using $this->request->getClientIp().

Step 2: Then copy the default laravel5 maintenance middleware in \app\Http\Middleware\MaintenanceMode.php or you can named as it is like CheckForMaintenanceMode.

<?php namespace App\Http\Middleware;
 
use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
 
class MaintenanceMode implements Middleware {
 
    protected $request;
    protected $app;
 
    public function __construct(Application $app, Request $request)
    {
        $this->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'])) // allowing the ip address
        {
            throw new HttpException(503);
        }
 
        return $next($request);
    }
 
}

Step 3: Now you need to open \app\Http\Kernel.php and comment the default middleware and add new middleware on it.

<?php namespace App\Http;
 
use Illuminate\Foundation\Http\Kernel as HttpKernel;
 
class Kernel extends HttpKernel {
 
     /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
     protected $middleware = [
          // 'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
          'App\Http\Middleware\MaintenanceMode',
          'Illuminate\Cookie\Middleware\EncryptCookies',
          'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
          'Illuminate\Session\Middleware\StartSession',
          'Illuminate\View\Middleware\ShareErrorsFromSession',
          'App\Http\Middleware\VerifyCsrfToken',
     ];
 
     /**
     * The application's route middleware.
     *
     * @var array
     */
     protected $routeMiddleware = [
          'auth' => 'App\Http\Middleware\Authenticate',
          'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
          'guest' => 'App\Http\Middleware\RedirectIfAuthenticated'
     ];
 
}

Step 4: You can change the view template for MaintenanceMode which is located in resources/views/errors/503.blade.php.

I hope you like the post and please comment below for suggestion , problem you faced during the configuration.

No comments:

Post a Comment

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...

Popular