Symfony 4 : ignore kernel events coming from debug toolbar

后端 未结 1 916
臣服心动
臣服心动 2021-01-15 02:40

I\'m quite new to Symfony so forgive me if it seems obvious for you :)

For my project, i need to perform some actions depending on the url. I use kernel events, more

相关标签:
1条回答
  • 2021-01-15 03:09

    So I did a bit more research. It's not that the kernel event is being fired twice but rather that once your original page is sent to the browser a bit of javascript initiates a second _wdt request for additional information. So you actually have two independent requests. You can see this by pressing F12 in your browser and then selecting the network tab and refreshing.

    It is easy enough to filter the debug request since the name of the route will always be _wdt. And you can also get the host directly from the request. Still want to check for the master request because eventually your code might trigger sub requests.

    public function onRequest(GetResponseEvent $event)
    {
        // Only master
        if (!$event->isMasterRequest()) {
            return;
        }
        $request = $event->getRequest();
    
        // Ignore toolbar
        if ($request->attributes->get('_route') === '_wdt') {
            return;
        }
    
        // Avoid parsing urls and other stuff, the request object should have
        // all the info you need
        $host = $request->getHost();
    
    }
    
    0 讨论(0)
提交回复
热议问题