Last active
March 11, 2023 18:31
-
-
Save Koalabaerchen/24ecf20f5324d31f2207 to your computer and use it in GitHub Desktop.
Maintenance Mode for Symfony
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
# saves sessions by (default) in /app/sessions/[environment], e.g. /app/sessions/dev or /app/sessions/prod | |
# be sure that the write permissions are set correctly for the folder. Should be the same as /cache/ or /logs/ | |
framework: | |
session: | |
save_path: %kernel.root_dir%/sessions/%kernel.environment% |
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 | |
/** | |
* Based on http://www.wenigersh.com/blog/post/maintenance-mode-for-symfony-2-applications | |
* | |
* Includes support of user permisson check (here: check if user has role ADMIN), so admins can still surf the site while in maintenance mode | |
* Be aware that if you clear the cache (to start maintenance in prod environment) your sessions might be cleared as well, so people get logged out | |
* The config.yml will move the sessions to /app/sessions/[environment] | |
* | |
* Also be aware that you need at least Symfony 2.6 for this permission check. For older versions the call is different | |
* @see http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements | |
* | |
* HTTP Response is a 503 Service Unavailable | |
* | |
* Don't forget to create the template that is called if you're in maintenance mode. See Comment in this file below | |
*/ | |
namespace Acme\DemoBundle\Listener; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
class MaintenanceListener | |
{ | |
private $container; | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
} | |
public function onKernelRequest(GetResponseEvent $event) | |
{ | |
// get maintenance parameters | |
$underMaintenanceUntil = $this->container->hasParameter('underMaintenanceUntil') ? $this->container->getParameter('underMaintenanceUntil') : false; | |
$maintenance = $this->container->hasParameter('maintenance') ? $this->container->getParameter('maintenance') : null; | |
$debug = in_array($this->container->get('kernel')->getEnvironment(), array('test')); | |
if ($maintenance && !$debug && !$this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { | |
$engine = $this->container->get('templating'); | |
// be sure to create the template and link to it. Location doesn't matter (for me), /app/Resources/views/ might be your best bet | |
$content = $engine->render('::maintenance.html.twig', array('underMaintenanceUntil' => $underMaintenanceUntil)); | |
$event->setResponse(new Response($content, 503)); | |
$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: | |
maintenance: false #turn it to true to enable maintenance, false if disable | |
underMaintenanceUntil: tommorow 8 AM # message you can display on your view, will be null if not set |
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
services: | |
acme.listener.maintenance: | |
class: Acme\DemoBundle\Listener\MaintenanceListener | |
arguments: | |
container: "@service_container" | |
tags: | |
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } |
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
is not found. Have an explanation ?
Use RequestEvent
instead. Here is the list of kernel event and its argument. https://symfony.com/doc/current/components/http_kernel.html#component-http-kernel-event-table
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
is not found. Have an explanation ?