我试图访问一个EventListener中的doctrine,它会测试一个数据库表,如果它无效,那么用户将被重定向到一个配置页,他将在那里解决问题!
测试将在每次调用控制器之前执行,所以我将使用Event kernel.controller:
[编辑:已解决,TESTNG并重新审视VERRY FINE]
<?php
namespace AdminBundle\EventListener;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
class BuildReferencesEventListener
{
/**
* @var EntityManager
*/
protected $em;
/**
* @var Router
*/
protected $router;
protected $references = [
1 => 'oxygen',
2 => 'pH',
3 => 'pCO2'
];
protected $redirect_route = 'references';
protected $requestStack;
protected $session;
public function __construct(EntityManager $entityManager, Router $router, RequestStack $requestStack, Session $session)
{
$this->em = $entityManager;
$this->router = $router;
$this->requestStack = $requestStack;
$this->session = $session;
}
public function onKernelController()
{
$em = $this->em;
$savedReferences = $em->getRepository('AdminBundle:ParamReference')->findAll();
$references = $this->references;
if (count($savedReferences) <= 0){
$this->redirect();
}
for ($i =0; $i<count($savedReferences); $i++){
if ($savedReferences[$i] !== $references[$i]) {
$this->redirect();
}
}
}
public function redirect()
{
$request = $this->requestStack->getCurrentRequest();
$route = $request->get('_route');
$this->session->getFlashBag()->add('warning', 'You need to setup the references for the parameters before starting the work');
if ($route != 'references'){
$url = $this->router->generate('references');
$redirect = new RedirectResponse($url);
$redirect->send();
}
}
}
这是services.yml
build.references:
class: AdminBundle\EventListener\BuildReferencesEventListener
arguments: ['@doctrine.orm.entity_manager', '@router', '@request_stack', '@session']
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
答案 0 :(得分:2)
在service.yml事件列表服务部分
中添加参数Your.service:
Class: AdminBundle\EventListener\BuildReferencesEventListener
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: kernel.event_listener, event: kernel.controller }
并在列表器中添加此__cunstruct代码:
namespace AdminBundle\EventListener;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Doctrine\ORM\EntityManager;
class BuildReferencesEventListener
{
protected $em;
function __construct(EntityManager $em)
{
$this->em = $em;
}
public function onKernelController(FilterControllerEvent $event)
{
//TO DO call doctrine
// $em = Enity Manager...
// $em->getRepository...
// I hope its clear enough what im trying to do
}
答案 1 :(得分:2)
在Symfony 3.4及更高版本中,默认情况下启用自动装配,因此您只需向服务的构造函数方法添加类型提示参数,并将私有属性添加到参数值。
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class BuildReferencesEventListener
{
private $em;
public __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function onKernelController(FilterControllerEvent $event)
{
$em = this->em;
// [...]
}
}
如果你需要 明确声明参数,您可以在服务配置中执行此操作:
# config/services.yaml
services:
# ... same code as before
# explicitly configure the service
AdminBundle\EventListener\BuildReferencesEventListener:
arguments:
$em: '@doctrine.orm.entity_manager_interface'
这可能对传递服务容器不知道的参数很有用。
要详细了解服务容器https://symfony.com/doc/3.4/service_container.html#injecting-services-config-into-a-service。