Laravel custom maintenance page dynamically
With the help of this tutorial you can create custom laravel maintenance page. Its will show custom page to all routes but allow all admin/*
1.create a middleware app\Http\Middleware\CustomMaintenanceMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CustomMaintenanceMiddleware
{
public function checkIfAdmin(){
if (request()->is('admin') || request()->is('admin/*')) {
return true;
}else{
return false;
}
}
public function handle(Request $request, Closure $next)
{
$is_maintence_mode = false;
if ($is_maintence_mode && !$this->checkIfAdmin()) {
$data = $this->getDataFromDatabase();
return response()->view('errors.custom_maintenance', compact('data'), 503);
}
return $next($request);
}
private function getDataFromDatabase()
{
return [];
}
}
2. Modify app\Exceptions\Handler.php
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Http\Middleware\CustomMaintenanceMiddleware;
use Throwable;
class Handler extends ExceptionHandler
{
protected $dontReport = [
//
];
public function render($request, Throwable $exception)
{
if ($this->isHttpException($exception)) {
if ($exception->getStatusCode() === 503 && app()->isDownForMaintenance()) {
$middleware = new CustomMaintenanceMiddleware();
return $middleware->handle($request, function ($request) {
return response('');
});
}
}
return parent::render($request, $exception);
}
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
public function register()
{
$this->reportable(function (Throwable $e) {
});
}
}
3. cretae a view Resources\views\errors\custom_maintenance.blade.php
Its custom page
4. Modify app\Http\Kernal.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\CustomMaintenanceMiddleware::class
],
'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];