Created
August 10, 2017 12:22
-
-
Save harini-ua/51d577023c7e8e7b6413a717b69c5dc5 to your computer and use it in GitHub Desktop.
Custom middleware Laravel 5 check for maintenance mode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
thanks . work fine. how i can use in package without remove original midalware?