Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

WARNING:

You are browsing the documentation for Symfony 4.1 which is not maintained anymore.

Consider upgrading your projects to Symfony 5.2.

How to Make the Web Debug Toolbar Auto-update After AJAX Requests

4.1 version
Unmaintained
4.1

How to Make the Web Debug Toolbar Auto-update After AJAX Requests

For single page applications it would be more convenient if the toolbar showed the information for the most recent AJAX request instead of the initial page load.

By setting the Symfony-Debug-Toolbar-Replace header to a value of 1 in the AJAX request, the toolbar will be automatically reloaded for the request. The header can be set on the response object:

$response->headers->set('Symfony-Debug-Toolbar-Replace', 1);

Only Setting the Header During Development

Ideally this header should only be set during development and not for production. This can be accomplished by setting the header in a kernel.response event listener:

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

// ...

public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();

    $response->headers->set('Symfony-Debug-Toolbar-Replace', 1);
}

See also

Read more about Symfony events.

If you are using Symfony Flex, you should define your event listener service in the config/services_dev.yml file so that it only exists in the dev environment.

See also

Read more on creating dev only services.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.