Skip to content

Instantly share code, notes, and snippets.

@jfcherng
Last active September 26, 2025 02:28
Show Gist options
  • Select an option

  • Save jfcherng/2fa6cccd68ad4cc84a31b974066db293 to your computer and use it in GitHub Desktop.

Select an option

Save jfcherng/2fa6cccd68ad4cc84a31b974066db293 to your computer and use it in GitHub Desktop.

Revisions

  1. jfcherng revised this gist May 11, 2023. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions KernelRequestSubscriber.php
    Original file line number Diff line number Diff line change
    @@ -35,9 +35,8 @@ public function onMaintenance(RequestEvent $event): void
    {
    /** @var bool $isMaintenance */
    $isMaintenance = \filter_var($_ENV['MAINTENANCE_MODE'] ?? '0', \FILTER_VALIDATE_BOOLEAN);
    $isCli = \PHP_SAPI === 'cli';

    if ($isMaintenance && !$isCli) {
    if ($isMaintenance) {
    $event->setResponse(new Response(
    $this->twig->render('maintenance.html.twig'),
    Response::HTTP_SERVICE_UNAVAILABLE,
  2. jfcherng created this gist Jun 9, 2020.
    1 change: 1 addition & 0 deletions .env
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    MAINTENANCE_MODE=0
    48 changes: 48 additions & 0 deletions KernelRequestSubscriber.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?php

    declare(strict_types=1);

    namespace App\EventSubscriber;

    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpKernel\Event\RequestEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    use Twig\Environment as TwigEnvironment;

    class KernelRequestSubscriber implements EventSubscriberInterface
    {
    private TwigEnvironment $twig;

    public function __construct(TwigEnvironment $twig)
    {
    $this->twig = $twig;
    }

    /**
    * {@inheritdoc}
    */
    public static function getSubscribedEvents(): array
    {
    return [
    KernelEvents::REQUEST => [
    ['onMaintenance', \PHP_INT_MAX - 1000],
    ],
    ];
    }

    public function onMaintenance(RequestEvent $event): void
    {
    /** @var bool $isMaintenance */
    $isMaintenance = \filter_var($_ENV['MAINTENANCE_MODE'] ?? '0', \FILTER_VALIDATE_BOOLEAN);
    $isCli = \PHP_SAPI === 'cli';

    if ($isMaintenance && !$isCli) {
    $event->setResponse(new Response(
    $this->twig->render('maintenance.html.twig'),
    Response::HTTP_SERVICE_UNAVAILABLE,
    ));
    $event->stopPropagation();
    }
    }
    }
    11 changes: 11 additions & 0 deletions maintenance.html.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <!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>