dginxreal/laravel-maintenance
Laravel package for putting your app into maintenance mode with customizable responses and exclusions. Helps show a friendly maintenance page, allow certain IPs or routes, and quickly toggle maintenance on/off during deployments or upgrades.
Installation
composer require dginxreal/laravel-maintenance
Publish the config file:
php artisan vendor:publish --provider="DGINXREAL\Maintenance\MaintenanceServiceProvider" --tag="config"
Enable Maintenance Mode
php artisan maintenance:enable
First Use Case: Scheduled Downtime
config/maintenance.php:
'downtime' => [
'enabled' => true,
'start' => '2024-01-01 00:00:00',
'end' => '2024-01-01 01:00:00',
],
php artisan maintenance:downtime
Dynamic Enablement via Middleware
Use the MaintenanceModeMiddleware to conditionally enable/disable maintenance based on logic (e.g., user roles, API keys):
protected function boot()
{
$this->middleware('maintenance', function ($request) {
return !$request->user()->isAdmin() && config('app.env') === 'production';
});
}
IP Whitelisting Allow specific IPs (e.g., your own) to bypass maintenance:
'allowed_ips' => [
'127.0.0.1',
'192.168.1.100',
],
Override Default Views Publish views:
php artisan vendor:publish --provider="DGINXREAL\Maintenance\MaintenanceServiceProvider" --tag="views"
Customize resources/views/vendor/maintenance/{page}.blade.php (e.g., maintenance.blade.php).
Dynamic Content Pass data to the maintenance view via middleware:
$response = app(\DGINXREAL\Maintenance\Facades\Maintenance::class)->response();
return $response->withData(['message' => 'System upgrade in progress!']);
Exclude API Routes Use route middleware groups:
Route::middleware(['api', 'maintenance:exclude'])->group(function () {
Route::prefix('api')->uses(...);
});
Configure exclusions in config/maintenance.php:
'excluded_routes' => ['api/*'],
API Status Endpoint Add a health check endpoint:
Route::get('/status', function () {
return response()->json([
'status' => app(\DGINXREAL\Maintenance\Facades\Maintenance::class)->isEnabled() ? 'down' : 'up',
]);
});
Cron Job Integration Use Laravel’s scheduler to toggle maintenance:
$schedule->command('maintenance:enable')->dailyAt('3:00');
$schedule->command('maintenance:disable')->dailyAt('4:00');
Graceful Degradation
Combine with queue:work to process pending jobs before downtime:
php artisan maintenance:enable --queue-flush
Caching Issues
php artisan route:clear
php artisan config:clear
config('cache.enabled') is false during testing if maintenance isn’t triggering.Middleware Order Matters
MaintenanceModeMiddleware before TrustProxies or VerifyCsrfToken to avoid bypassing maintenance.Downtime Timezone
config/maintenance.php:
'timezone' => 'America/New_York',
Queue Jobs During Downtime
--queue-flush to process them first:
php artisan maintenance:enable --queue-flush
dd(app(\DGINXREAL\Maintenance\Facades\Maintenance::class)->isEnabled());
app/Http/Middleware/MaintenanceModeMiddleware.php:
Log::debug('Maintenance check triggered for IP: ' . $request->ip());
Custom Maintenance Logic
Extend the MaintenanceService:
namespace App\Services;
use DGINXREAL\Maintenance\MaintenanceService;
class CustomMaintenanceService extends MaintenanceService
{
public function isEnabled()
{
if (parent::isEnabled()) {
return true;
}
// Add custom logic (e.g., check database flag)
return DB::table('maintenance_flags')->exists();
}
}
Bind it in AppServiceProvider:
$this->app->bind(
\DGINXREAL\Maintenance\Contracts\Maintenance::class,
App\Services\CustomMaintenanceService::class
);
Event-Based Maintenance Listen for maintenance events to trigger side effects (e.g., notifications):
\DGINXREAL\Maintenance\Events\MaintenanceEnabled::class => [
\App\Listeners\SendDowntimeNotification::class,
],
Multi-Tenant Maintenance Scope maintenance per tenant using a custom guard:
'tenant_guard' => 'tenant',
Implement TenantGuard in config/maintenance.php.
How can I help you explore Laravel packages today?