Reputation: 352
I have a config table with a 'web_enabled' key, when true I want to show the route requested but when false I want to show a 'Site in maintenance' page. Obviously this check has to be performed before any route action.
I have been reading about Events and Listeners but I don't see how to implement the access to the doctrine and template.
Thanks for the help.
Upvotes: 1
Views: 533
Reputation: 352
This is the solution I implemented finally, differs from the proposed by Alsatian because I don't use a parameter in the service.yml. Is just a question of taste, nothing else.
in app/config/services.yml
services:
app.request_listener:
class: AppBundle\EventListener\RequestListener
arguments: ["@doctrine.orm.entity_manager","@templating"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelController }
in src/AppBundle/EventListener/RequestListener.php
namespace AppBundle\EventListener;
use \Symfony\Component\HttpKernel\Event\GetResponseEvent;
use \Symfony\Component\HttpFoundation\Response;
class RequestListener
{
private $em;
private $templating;
public function __construct($em, $templating)
{
$this->em = $em;
$this->templating = $templating;
}
public function onKernelController(GetResponseEvent $event)
{
if ( !$this->configKey = $this->em->getRepository('AppBundle:Config')->getconfig('web_enabled') )
$event->setResponse($this->templating->renderResponse('default/construction.html.twig'));
}
}
and in src/AppBundle/Repository/ConfigRepository.php explaining the getconfig method:
namespace AppBundle\Repository;
class ConfigRepository extends \Doctrine\ORM\EntityRepository
{
public function getconfig( $config_name )
{
$config = $this->getEntityManager()
->createQuery('SELECT p.config_value FROM AppBundle:Config p WHERE p.config_name = :config_name')
->setParameter('config_name', $config_name)
->getResult();
if (sizeof($config)){
return $config[0]['config_value'];
}else{
return false;
}
}
}
Hope this helps.
Upvotes: 1
Reputation: 3135
You have just to inject both EntityManager and Templating in your listener :
Definition as service :
# src/AppBundle/Ressources/config/services.yml
services:
app.request_listener:
class: AppBundle\EventListener\RequestListener
arguments: ["%web_enabled%","@doctrine.orm.entity_manager","@templating"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Listener :
namespace AppBundle\EventListener;
use \Symfony\Component\HttpKernel\Event\GetResponseEvent;
use \Symfony\Component\HttpFoundation\Response;
class RequestListener
{
private $configKey;
private $em;
private $templating;
public __construct($configKey, $em, $templating)
{
$this->configKey = $configKey;
$this->em = $em;
$this->templating = $templating;
}
public function onKernelRequest(GetResponseEvent $event)
{
if(!$this->configKey){
$var = $this->em->getRepository('AppBundle:MyEntity')->findOne(1);
$event->setResponse($this->templating->renderResponse('my_template.html.twig',array('var'=>$var));
}
}
}
Upvotes: 0