Php Symfony-在onKernelRequest中使用细枝终止会话

Php Symfony-在onKernelRequest中使用细枝终止会话,php,symfony,symfony4,Php,Symfony,Symfony4,我在symfony 4.3中遇到了一个奇怪的问题(也在4.2中测试了它-相同的行为)-我正在使用EventListener来处理请求-下面是代码: <?php namespace App\EventListener; use App\Entity\Company; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Sym

我在symfony 4.3中遇到了一个奇怪的问题(也在4.2中测试了它-相同的行为)-我正在使用EventListener来处理请求-下面是代码:

<?php

namespace App\EventListener;

use App\Entity\Company;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;

class ShopListener implements EventSubscriberInterface
{

    /** @var EntityManagerInterface */
    protected $em;

    /** @var Environment */
    protected $twig;

    public function __construct(EntityManagerInterface $entityManager, Environment $twig)
    {
        $this->em=$entityManager;
        $this->twig=$twig;
    }

    public function onKernelRequest(RequestEvent $event)
    {

        if($event->isMasterRequest()===false) {
            return;
        }

        /** @var Request $request */
        $request=$event->getRequest();

        $subDomain=$request->attributes->get('domain');
        if($subDomain===null) {
            return;
        }

        $company=$this->em->getRepository(Company::class)->findOneBy([
            'subDomain' => $subDomain,
        ]);

        if($company instanceof Company && $company->shopIsOnline()) {
            $request->attributes->set('company',$company);
            return;
        }

        $event->setResponse(
            new Response($this->twig->render('page/shop_not_found.html.twig'),404)
        );

    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::REQUEST => ['onKernelRequest',0],
        ];
    }

}

会话是由
内核上的
Symfony\Component\FrameworkBundle\EventListener\SessionListener
监听器创建的。请求
事件(优先级为128)。
此事件有一个特定的行为:如果侦听器设置了响应,“进程直接跳到kernel.Response事件”以引用。我怀疑这可能会引起问题。

尝试将您的侦听器设置为优先级<0(我让您尝试了许多),并请检查探查器“事件”部分的顺序(
/\u profiler/latest?panel=Events
)。

找到了解决方案-问题是在构造函数中注入细枝环境-没有细枝,一切都按预期工作。我想,在这个阶段加载细枝环境会对会话产生影响(比如加载太早)。 我将侦听器移动到onKernelController并对其进行了修改:

<?php

namespace App\EventListener;

use App\Entity\Company;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;

class ShopListener implements EventSubscriberInterface
{

    /** @var EntityManagerInterface */
    protected $em;

    /** @var Environment */
    protected $twig;


    public function __construct(EntityManagerInterface $entityManager, Environment $twig)
    {
        $this->em=$entityManager;
        $this->twig=$twig;
    }


    public function onKernelController(ControllerEvent $controllerEvent)
    {

        if($controllerEvent->isMasterRequest()===false) {
            return;
        }

        /** @var Request $request */
        $request=$controllerEvent->getRequest();

        $subDomain = $request->attributes->get('domain');
        if($subDomain===null) {
            return;
        }

        $company=$this->em->getRepository(Company::class)->findOneBy([
            'subDomain' => $subDomain,
        ]);

        if($company instanceof Company && $company->shopIsOnline()) {
            $request->attributes->set('company',$company);
            return;
        }

        $controllerEvent->setController(
            function() {
                return new Response($this->twig->render('page/shop_not_found.html.twig'),404);
            }
        );
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::CONTROLLER => ['onKernelController',128],
        ];
    }
}

感谢您的提示,已经尝试了负优先级。我明白,当我设置响应时,我的控制器不再被调用。问题发生在更早返回时(例如,当子域为null时)。哦,这是完全不同的。我不太明白这怎么会发生。当问题发生时,你能发布“事件”面板的截图吗?