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 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题