Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save baorv/83e1fd8a246397812fcd1bd3a3d10ee4 to your computer and use it in GitHub Desktop.
Save baorv/83e1fd8a246397812fcd1bd3a3d10ee4 to your computer and use it in GitHub Desktop.
Custom middleware Laravel 5 check for maintenance mode.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Routing\Route;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Original;
class CheckForMaintenanceMode extends Original
{
protected $excludedNames = [];
protected $except = ['admin/*', 'stats'];
protected $excludedIPs = [];
protected function shouldPassThrough($request)
{
foreach ($this->except as $except) {
if ($except !== '/') {
$except = trim($except, '/');
}
if ($request->is($except)) {
return true;
}
}
return false;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function handle($request, Closure $next)
{
if ($this->app->isDownForMaintenance()) {
$response = $next($request);
if (in_array($request->ip(), $this->excludedIPs)) {
return $response;
}
$route = $request->route();
if ($route instanceof Route) {
if (in_array($route->getName(), $this->excludedNames)) {
return $response;
}
}
if ($this->shouldPassThrough($request))
{
return $response;
}
throw new HttpException(503);
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment