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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵