Skip to content

Instantly share code, notes, and snippets.

@harini-ua
Created August 10, 2017 12:22
Show Gist options
  • Save harini-ua/51d577023c7e8e7b6413a717b69c5dc5 to your computer and use it in GitHub Desktop.
Save harini-ua/51d577023c7e8e7b6413a717b69c5dc5 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);
}
}
@vahidalvandi
Copy link

thanks . work fine. how i can use in package without remove original midalware?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment