dongyi5425 2019-06-02 11:35
浏览 59
已采纳

Symfony - 在onKernelRequest中使用twig杀死会话

I have a strange issue in symfony 4.3 (also tested it in 4.2 - same behaviour) - I am using an EventListener to process a request - heres the code:

<?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],
        ];
    }

}

After registering that listener, $request->getSession() is always null in my controller (toolbar also notices, that there is no session registered). When deregistering it, the session is there, but the logic in the listener is skipped. I have tried to play around with the priority to ensure, there's no other listener which interferes. It seems, that already registering that event kills the session (even if onKernelRequest is empty), which is hard to believe. What am I missing?

  • 写回答

2条回答 默认 最新

  • doukuang1950 2019-06-05 17:34
    关注

    Found the solution - problem is the injection of the twig-environment in the constructor - without twig everything works as expected. I guess, loading the twig-environment at this stage does something to the session (like loading it too early). I moved the listener to onKernelController and modified it:

    <?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],
            ];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭