2019-06-02 11:35
浏览 58

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:


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)

    public function onKernelRequest(RequestEvent $event)

        if($event->isMasterRequest()===false) {

        /** @var Request $request */

        if($subDomain===null) {

            'subDomain' => $subDomain,

        if($company instanceof Company && $company->shopIsOnline()) {

            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条回答 默认 最新

相关推荐 更多相似问题