cvr/under-maintenance-bundle
Installation:
composer require cvr/under-maintenance-bundle
Register the bundle in config/bundles.php:
Cvr\UnderMaintenanceBundle\UnderMaintenanceBundle::class => ['all' => true],
Enable Maintenance Mode:
Add to .env:
MAINTENANCE=1
MAINTENANCE_TOKEN=your_secure_token_here
First Use Case:
MAINTENANCE=1.http://your-site.com?maintenance=your_secure_token_here
Temporary Maintenance Mode:
.env and disable post-deployment:
sed -i 's/MAINTENANCE=1/MAINTENANCE=0/' .env
Custom Maintenance Pages:
templates/bundles/UnderMaintenanceBundle/maintenance.html.twig
php bin/console cache:clear
Token-Based Access Control:
Integration with Deployment Scripts:
# Enable maintenance before deployment
sed -i 's/MAINTENANCE=0/MAINTENANCE=1/' .env
git add .env && git commit -m "Enable maintenance mode"
# Disable after deployment
sed -i 's/MAINTENANCE=1/MAINTENANCE=0/' .env
git add .env && git commit -m "Disable maintenance mode"
Service Provider Binding:
If using Laravel’s service container, bind the bundle’s services explicitly in AppServiceProvider:
$this->app->bind('under_maintenance', function () {
return new \Cvr\UnderMaintenanceBundle\Service\MaintenanceService();
});
Route Middleware: Extend functionality by creating a custom middleware to check maintenance status:
namespace App\Http\Middleware;
use Closure;
use Symfony\Component\HttpFoundation\Request;
class MaintenanceCheck
{
public function handle(Request $request, Closure $next)
{
if (config('under_maintenance.enabled') && !$request->query->has('maintenance')) {
return redirect()->route('maintenance.page');
}
return $next($request);
}
}
Cache Invalidation:
php bin/console cache:clear) after template overrides will result in the old template being displayed.Token Exposure:
.env is fine, but avoid logging or committing them to version control.Symfony vs. Laravel Quirks:
config/bundles.php file exists (create it if missing) and is properly formatted.Template Override Path:
templates/bundles/UnderMaintenanceBundle/maintenance.html.twig
Token Validation:
Check Maintenance Status:
Verify the .env values are loaded correctly:
php bin/console debug:config under_maintenance
Should output:
enabled: true
token: your_secure_token_here
Template Not Updating:
Ensure the template file exists and permissions allow reading. Check Symfony’s cache directory (var/cache/dev/) for compiled templates.
Token Bypass Failing:
Confirm the query parameter is named exactly maintenance (case-sensitive in Symfony).
Custom Logic:
Extend the bundle by overriding the MaintenanceListener class:
namespace App\EventListener;
use Cvr\UnderMaintenanceBundle\Event\MaintenanceEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class CustomMaintenanceListener
{
public function onMaintenance(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->query->get('maintenance') === 'custom_token') {
// Custom logic (e.g., redirect to a specific page)
}
}
}
Register the listener in config/services.yaml (Symfony) or AppServiceProvider (Laravel).
Dynamic Tokens:
Generate tokens dynamically (e.g., via API) by overriding the MaintenanceService:
namespace App\Service;
use Cvr\UnderMaintenanceBundle\Service\MaintenanceService as BaseService;
class CustomMaintenanceService extends BaseService
{
public function getToken()
{
return $this->generateSecureToken(); // Custom logic
}
}
Multi-Token Support: Allow multiple tokens by modifying the listener to check against an array:
$validTokens = ['token1', 'token2', 'token3'];
if (in_array($request->query->get('maintenance'), $validTokens)) {
// Allow access
}
How can I help you explore Laravel packages today?