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 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号