Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the debug token on exception?

I have written my own ExceptionController and want to inject the debug token as part of the output.
I had a look on the WebProfilerToolbarListener, which is similar to my case but it seems that the exception/error response has another lifecycle.

The ProfilerCollector is always called after my listener, which is on the response event.
My Listener needs to be called after the ProfilerCollector.

How can I achieve it?

enter image description here

like image 532
CSchulz Avatar asked Dec 10 '25 05:12

CSchulz


1 Answers

I have tried to use different priorities, but it seems that you need to clear the cache completly. It isn't sufficient to reload the page in development mode.

After changing the priority to -150 like below it works.

profiler_link_listener:
    class:Nerdhive\TheNetwork\TwigBundle\EventListener\ProfilerLinkListener
    priority: -150
    tags:
        - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

enter image description here

You can retrieve the debug token like below:

public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();
    $token = $response->headers->get('X-Debug-Token');
    // Do here your stuff
}

In general -129 should also work regarding the WebDebugToolbarListener or documentation:

public static function getSubscribedEvents()
{
    return array(
        KernelEvents::RESPONSE => array('onKernelResponse', -128),
    );
}
like image 182
CSchulz Avatar answered Dec 11 '25 21:12

CSchulz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!