Skip to content

Instantly share code, notes, and snippets.

@akky
Forked from jfcherng/.env
Last active December 21, 2024 09:03
Show Gist options
  • Save akky/246bfe851f5684eb437e71d6ce856b74 to your computer and use it in GitHub Desktop.
Save akky/246bfe851f5684eb437e71d6ce856b74 to your computer and use it in GitHub Desktop.
maintenance mode - checked working on Symfony 7.2
# if you want to turn on maintenance-mode, use this
#MAINTENANCE_MODE=true
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Site temporarily under maintenance</title>
</head>
<body>
<h1>Site temporarily under maintenance</h1>
<p>Sorry for the inconvenience, we will get back soon!</p>
</body>
</html>
<?php
declare(strict_types=1);
namespace App\EventListener;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
final class MaintenanceKernelRequestListener
{
public function __construct(
private Environment $twig,
#[Autowire(env: 'default:app.default_maintenance_mode:bool:MAINTENANCE_MODE')]
private bool $isMaintenance,
) {}
#[AsEventListener(event: KernelEvents::REQUEST, priority: PHP_INT_MAX - 1000)]
public function onKernelRequest(RequestEvent $event): void
{
if (!$this->isMaintenance) {
return;
}
$event->setResponse(new Response(
$this->twig->render('maintenance.html.twig'),
Response::HTTP_SERVICE_UNAVAILABLE,
));
$event->stopPropagation();
}
}
parameters:
app.default_maintenance_mode: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment