Skip to content

Instantly share code, notes, and snippets.

@Koalabaerchen
Last active March 11, 2023 18:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Koalabaerchen/24ecf20f5324d31f2207 to your computer and use it in GitHub Desktop.
Save Koalabaerchen/24ecf20f5324d31f2207 to your computer and use it in GitHub Desktop.
Maintenance Mode for Symfony
# 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%
<?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();
}
}
}
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
services:
acme.listener.maintenance:
class: Acme\DemoBundle\Listener\MaintenanceListener
arguments:
container: "@service_container"
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
@Dahkenangnon
Copy link

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
is not found. Have an explanation ?

@fd6130
Copy link

fd6130 commented Oct 14, 2020

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