Installation
composer require coa/maintenance-bundle
Add to config/app.php under providers:
CoteOuest\MaintenanceBundle\MaintenanceBundle::class,
Publish the config file:
php artisan vendor:publish --provider="CoteOuest\MaintenanceBundle\MaintenanceBundle" --tag=config
Enable Maintenance Mode Run the Artisan command:
php artisan maintenance:enable
This immediately activates maintenance mode, showing a default "Under Maintenance" page.
Customize the Page Override the default template by publishing the view:
php artisan vendor:publish --provider="CoteOuest\MaintenanceBundle\MaintenanceBundle" --tag=views
Edit resources/views/maintenance.blade.php to customize the message, styling, or add a countdown.
First Use Case: Scheduled Downtime Enable maintenance mode before a deployment or database migration:
php artisan maintenance:enable --message="Site undergoing maintenance. Back soon!"
Disable it afterward:
php artisan maintenance:disable
Automated Deployment Scripts Integrate with deployment tools (e.g., GitHub Actions, Jenkins) to enable maintenance mode before deployment and disable it post-deployment:
# In deploy script
php artisan maintenance:enable --message="Deploying updates..."
# Run deployment commands...
php artisan maintenance:disable
Environment-Based Configuration
Use the config/maintenance.php file to define environment-specific settings:
'environments' => [
'local' => false, // Disable in local
'staging' => true, // Enable in staging
'production' => true,
],
Enable/disable via:
php artisan maintenance:enable --env=production
Custom Middleware for Partial Maintenance Extend the bundle to allow maintenance mode for specific routes or IPs:
// app/Http/Kernel.php
protected $routeMiddleware = [
'maintenance.partial' => \CoteOuest\MaintenanceBundle\Http\Middleware\PartialMaintenance::class,
];
Apply to routes:
Route::middleware(['maintenance.partial'])->group(function () {
// Routes to exclude from maintenance
});
API-Specific Maintenance
Use the maintenance:enable command with the --api flag to show a JSON response for API requests:
php artisan maintenance:enable --api --message='{"status":"maintenance","message":"Service unavailable"}'
config('app.debug') to disable maintenance in debug mode.storage/logs/laravel.log for auditing:
\Log::info('Maintenance mode enabled by ' . auth()->user()?->name);
Caching Issues
php artisan route:clear
config('cache.enabled') is false during maintenance if using cached routes.Middleware Conflicts
MaintenanceMiddleware) runs early in the stack. If other middleware (e.g., auth) fails, it may not trigger the maintenance page. Test thoroughly.Environment Mismatches
environments config in maintenance.php is case-sensitive. Ensure it matches your .env values exactly.Custom Views Not Loading
maintenance.blade.php, ensure the file exists in resources/views/ and the view is published correctly. Check for typos in the filename.Check Status Verify maintenance mode is active:
php artisan maintenance:status
Output: Maintenance mode is enabled.
Log Middleware Execution
Add debug logs to the middleware (vendor/coa/maintenance-bundle/src/Http/Middleware/MaintenanceMiddleware.php) to trace execution:
\Log::debug('MaintenanceMiddleware executed for ' . $request->path());
Test API Responses
Use curl or Postman to test API responses:
curl -i http://your-site.test/api/endpoint
Expect a 503 Service Unavailable response if enabled.
Custom Error Pages
Extend the MaintenanceController to return dynamic content:
namespace App\Http\Controllers;
use CoteOuest\MaintenanceBundle\Http\Controllers\MaintenanceController as BaseController;
class MaintenanceController extends BaseController
{
public function show()
{
return response()->view('custom.maintenance', [], 503);
}
}
Override the controller binding in AppServiceProvider:
public function register()
{
$this->app->bind(
\CoteOuest\MaintenanceBundle\Http\Controllers\MaintenanceController::class,
App\Http\Controllers\MaintenanceController::class
);
}
IP Whitelisting Modify the middleware to allow specific IPs:
// In MaintenanceMiddleware
public function handle($request, Closure $next)
{
$ips = ['127.0.0.1', '192.168.1.100']; // Whitelisted IPs
if (!in_array($request->ip(), $ips) && $this->isEnabled()) {
return $this->showMaintenancePage($request);
}
return $next($request);
}
Event Listeners Listen for maintenance mode changes to trigger side effects (e.g., notifications):
// app/Providers/EventServiceProvider.php
protected $listen = [
\CoteOuest\MaintenanceBundle\Events\MaintenanceEnabled::class => [
\App\Listeners\SendMaintenanceNotification::class,
],
];
Configuration Overrides Dynamically override settings via environment variables:
// config/maintenance.php
'message' => env('MAINTENANCE_MESSAGE', 'Under maintenance.'),
Set in .env:
MAINTENANCE_MESSAGE="We're back! Check our blog for updates."
How can I help you explore Laravel packages today?