From 495e978eef4dbc600379982464ee6fb196b2c2f1 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Fri, 17 Jan 2025 17:15:32 +0100 Subject: [PATCH] docs: web profiler ajax_replace --- profiler.rst | 61 +++++++++++++++++------- reference/configuration/web_profiler.rst | 9 ++++ 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/profiler.rst b/profiler.rst index 57d412472ba..961a66d1c4f 100644 --- a/profiler.rst +++ b/profiler.rst @@ -217,9 +217,48 @@ user by dynamically rewriting the current page rather than loading entire new pages from a server. By default, the debug toolbar displays the information of the initial page load -and doesn't refresh after each AJAX request. However, you can set the -``Symfony-Debug-Toolbar-Replace`` header to a value of ``'1'`` in the response to -the AJAX request to force the refresh of the toolbar:: +and doesn't refresh after each AJAX request. However, you can configure the +toolbar to be refreshed after each AJAX request by enabling ``ajax_replace`` in the +``web_profiler`` configuration: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/web_profiler.yaml + web_profiler: + toolbar: + ajax_replace: true + + .. code-block:: xml + + + + + + + + + + + .. code-block:: php + + // config/packages/web_profiler.php + use Symfony\Config\WebProfilerConfig; + + return static function (WebProfilerConfig $profiler): void { + $profiler->toolbar() + ->ajaxReplace(false); + }; + +If you need a more sophisticated solution, you can set the +``Symfony-Debug-Toolbar-Replace`` header to a value of ``'1'`` in the response +yourself:: $response->headers->set('Symfony-Debug-Toolbar-Replace', '1'); @@ -228,31 +267,21 @@ production. To do that, create an :doc:`event subscriber ` and listen to the :ref:`kernel.response ` event:: + use Symfony\Component\DependencyInjection\Attribute\When; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\KernelInterface; // ... + #[When(env: 'dev')] class MySubscriber implements EventSubscriberInterface { - public function __construct( - private KernelInterface $kernel, - ) { - } - // ... public function onKernelResponse(ResponseEvent $event): void { - if (!$this->kernel->isDebug()) { - return; - } - - $request = $event->getRequest(); - if (!$request->isXmlHttpRequest()) { - return; - } + // Your custom logic here $response = $event->getResponse(); $response->headers->set('Symfony-Debug-Toolbar-Replace', '1'); diff --git a/reference/configuration/web_profiler.rst b/reference/configuration/web_profiler.rst index 93c65621999..b68fa3516c0 100644 --- a/reference/configuration/web_profiler.rst +++ b/reference/configuration/web_profiler.rst @@ -56,8 +56,17 @@ on the given link to perform the redirect. toolbar ~~~~~~~ +enabled +....... **type**: ``boolean`` **default**: ``false`` It enables and disables the toolbar entirely. Usually you set this to ``true`` in the ``dev`` and ``test`` environments and to ``false`` in the ``prod`` environment. + +ajax_replace +............ +**type**: ``boolean`` **default**: ``false`` + +If you set this option to ``true``, the toolbar is replaced on AJAX requests. +This only works in combination with an enabled toolbar.