Installation Run:
composer require arkanii/maintenance-bundle
For non-Flex projects, manually add the bundle to config/bundles.php:
Arkanii\MaintenanceBundle\ArkaniiMaintenanceBundle::class => ['all' => true],
First Use Case Enable maintenance mode via CLI:
php bin/console maintenance:enable
Disable it with:
php bin/console maintenance:disable
Where to Look First
php bin/console list → Filter for maintenance: commands.config/packages/arkanii_maintenance.yaml (auto-generated).templates/maintenance.html.twig.Toggle Maintenance Mode Use the console commands for quick toggling:
# Enable with custom message
php bin/console maintenance:enable --message="Under maintenance"
# Disable
php bin/console maintenance:disable
Conditional Maintenance
Integrate with Symfony’s EventDispatcher to trigger maintenance dynamically:
// src/EventListener/MaintenanceListener.php
use Arkanii\MaintenanceBundle\Event\MaintenanceEvent;
class MaintenanceListener {
public function onMaintenance(MaintenanceEvent $event) {
if ($event->isEnabled() && $event->getUser()->isAdmin()) {
$event->setEnabled(false); // Allow admins
}
}
}
Register in services.yaml:
services:
App\EventListener\MaintenanceListener:
tags: ['kernel.event_listener', { event: 'arkanii.maintenance', method: 'onMaintenance' }]
Custom Templates Override the default Twig template by creating:
templates/ArkaniiMaintenanceBundle/maintenance.html.twig
Extend the base template with custom styling or logic.
API Maintenance
Exclude API routes by configuring allowed_paths in config/packages/arkanii_maintenance.yaml:
arkanii_maintenance:
allowed_paths:
- ^/api/
HttpKernel in Laravel via symfony/http-kernel to leverage this bundle.staging/production:
# config/packages/arkanii_maintenance.yaml
arkanii_maintenance:
enabled: '%kernel.environment% == "prod"'
Caching Issues
php bin/console cache:clear
X-Symfony-Debug-Token) are respected.Route Overrides
maintenance route. Avoid naming conflicts with existing routes.Twig Auto-Reload
php bin/console server:restart
IP Whitelisting
// src/Middleware/AllowMaintenanceForIPs.php
public function handle(Request $request, Closure $next) {
if (in_array($request->ip(), ['192.168.1.1', '127.0.0.1']) && $this->maintenance->isEnabled()) {
$this->maintenance->setEnabled(false);
}
return $next($request);
}
maintenance:status command to verify mode:
php bin/console maintenance:status
APP_DEBUG=1) to log maintenance events in var/log/dev.log.Custom Events
Extend the MaintenanceEvent to add metadata (e.g., reason, scheduledUntil):
// src/Event/MaintenanceCustomEvent.php
class MaintenanceCustomEvent extends MaintenanceEvent {
private $scheduledUntil;
public function __construct(bool $enabled, string $message, \DateTime $scheduledUntil) {
parent::__construct($enabled, $message);
$this->scheduledUntil = $scheduledUntil;
}
}
Middleware Integration
Use the bundle’s MaintenanceChecker service in custom middleware:
use Arkanii\MaintenanceBundle\MaintenanceChecker;
class CustomMaintenanceMiddleware {
public function __construct(private MaintenanceChecker $checker) {}
public function handle(Request $request, Closure $next) {
if ($this->checker->isEnabled()) {
return new Response('Custom maintenance message', 503);
}
return $next($request);
}
}
Configuration Overrides Dynamically override settings via environment variables:
# .env
MAINTENANCE_MESSAGE="System upgrade in progress"
MAINTENANCE_ENABLED="true"
# config/packages/arkanii_maintenance.yaml
arkanii_maintenance:
message: '%env(MAINTENANCE_MESSAGE)%'
enabled: '%env(bool:MAINTENANCE_ENABLED)%'
CronExpression to auto-disable after a duration:
// src/Command/ScheduledMaintenanceCommand.php
use Symfony\Component\Cron\CronExpression;
class ScheduledMaintenanceCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
$maintenance = $this->getContainer()->get('arkanii_maintenance.maintenance');
if ($maintenance->isEnabled() && CronExpression::create('0 10 * * *')->isDue()) {
$maintenance->disable();
}
}
}
%kernel.environment% to tailor messages per environment:
arkanii_maintenance:
message: >
%kernel.environment% == 'prod' ? 'Production maintenance' : 'Staging work in progress'
How can I help you explore Laravel packages today?