In Laravel 4 you can bypass some IP addresses for Laravel Maintenance Mode (php artisan down
) by doing this:
App::down(function()
{
if ( !in_array(Request::getClientIp(), ['192.168.0.1']))
{
return Response::view('maintenance', [], 503);
}
});
You can also provide a config file maintenance.php with list of all the ip addresses to allow access to your application when in maintenance mode:
<?php
return [
/*
|--------------------------------------------------------------------------
| Allowed IP Addresses
|--------------------------------------------------------------------------
| Include an array of IP addresses or ranges that are allowed access to the app when
| it is in maintenance mode.
|
| Supported formats:
|
*/
'allowed_ips' => [
'10.0.2.2',
'10.2.*.*',
'10.0.2.3 - 10.0.2.45',
'10.0.3.0-10.3.3.3'
],
];
My question is, How do i achieve this in Laravel 5?
Create new middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
class CheckForMaintenanceMode
{
protected $request;
protected $app;
public function __construct(Application $app, Request $request)
{
$this->app = $app;
$this->request = $request;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->app->isDownForMaintenance() &&
!in_array($this->request->getClientIp(), ['::1','another_IP']))
{
throw new HttpException(503);
}
return $next($request);
}
}
The '::1'
is your own IP assuming your in localhost, if not the specify your IP. You can excluded multiple IP in the array. check Excluding your IP Address in Maintenance Mode (php artisan down) in Laravel 5