-
-
Save akky/246bfe851f5684eb437e71d6ce856b74 to your computer and use it in GitHub Desktop.
maintenance mode - checked working on Symfony 7.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# if you want to turn on maintenance-mode, use this | |
#MAINTENANCE_MODE=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
app.default_maintenance_mode: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment