Symfony 3.4:在 EventListener 中访问原则

Symfony 3.4: Access doctrine within an EventListener

我正在尝试访问测试数据库 table 的 EventListener 中的学说,如果它无效,那么用户将被重定向到一个配置页面,他将在其中解决问题![​​=12= ]

测试将在每次调用控制器之前执行,所以我将使用事件 kernel.controller:

[已编辑:已解决,测试和重定向非常好]

<?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 }

在您的 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
    }

在 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