Skip to content

Commit 3c1398a

Browse files
committed
docs: web profiler ajax_replace
1 parent 1b385bb commit 3c1398a

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

profiler.rst

+45-16
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,48 @@ user by dynamically rewriting the current page rather than loading entire new
217217
pages from a server.
218218

219219
By default, the debug toolbar displays the information of the initial page load
220-
and doesn't refresh after each AJAX request. However, you can set the
221-
``Symfony-Debug-Toolbar-Replace`` header to a value of ``'1'`` in the response to
222-
the AJAX request to force the refresh of the toolbar::
220+
and doesn't refresh after each AJAX request. However, you can configure the
221+
toolbar to be refreshed after each AJAX request by enabling ``ajax_replace`` in the
222+
``web_profiler`` configuration:
223+
224+
.. configuration-block::
225+
226+
.. code-block:: yaml
227+
228+
# config/packages/web_profiler.yaml
229+
web_profiler:
230+
toolbar:
231+
ajax_replace: true
232+
233+
.. code-block:: xml
234+
235+
<!-- config/packages/web_profiler.xml -->
236+
<?xml version="1.0" ?>
237+
<container xmlns="http://symfony.com/schema/dic/services"
238+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
239+
xmlns:framework="http://symfony.com/schema/dic/symfony"
240+
xmlns:web-profiler="http://symfony.com/schema/dic/webprofiler"
241+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
242+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
243+
244+
<web-profiler:config>
245+
<web-profiler:toolbar ajax-replace="true"/>
246+
</web-profiler:config>
247+
</container>
248+
249+
.. code-block:: php
250+
251+
// config/packages/web_profiler.php
252+
use Symfony\Config\WebProfilerConfig;
253+
254+
return static function (WebProfilerConfig $profiler): void {
255+
$profiler->toolbar()
256+
->ajaxReplace(true);
257+
};
258+
259+
If you need a more sophisticated solution, you can set the
260+
``Symfony-Debug-Toolbar-Replace`` header to a value of ``'1'`` in the response
261+
yourself::
223262

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

@@ -228,31 +267,21 @@ production. To do that, create an :doc:`event subscriber </event_dispatcher>`
228267
and listen to the :ref:`kernel.response <component-http-kernel-kernel-response>`
229268
event::
230269

270+
use Symfony\Component\DependencyInjection\Attribute\When;
231271
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
232272
use Symfony\Component\HttpKernel\Event\ResponseEvent;
233273
use Symfony\Component\HttpKernel\KernelInterface;
234274

235275
// ...
236276

277+
#[When(env: 'dev')]
237278
class MySubscriber implements EventSubscriberInterface
238279
{
239-
public function __construct(
240-
private KernelInterface $kernel,
241-
) {
242-
}
243-
244280
// ...
245281

246282
public function onKernelResponse(ResponseEvent $event): void
247283
{
248-
if (!$this->kernel->isDebug()) {
249-
return;
250-
}
251-
252-
$request = $event->getRequest();
253-
if (!$request->isXmlHttpRequest()) {
254-
return;
255-
}
284+
// Your custom logic here
256285

257286
$response = $event->getResponse();
258287
$response->headers->set('Symfony-Debug-Toolbar-Replace', '1');

reference/configuration/web_profiler.rst

+13
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,21 @@ on the given link to perform the redirect.
5656
toolbar
5757
~~~~~~~
5858

59+
enabled
60+
.......
5961
**type**: ``boolean`` **default**: ``false``
6062

6163
It enables and disables the toolbar entirely. Usually you set this to ``true``
6264
in the ``dev`` and ``test`` environments and to ``false`` in the ``prod``
6365
environment.
66+
67+
ajax_replace
68+
............
69+
**type**: ``boolean`` **default**: ``false``
70+
71+
If you set this option to ``true``, the toolbar is replaced on AJAX requests.
72+
This only works in combination with an enabled toolbar.
73+
74+
.. versionadded:: 7.3
75+
76+
The ``ajax_replace`` configuration option was introduced in Symfony 7.3.

0 commit comments

Comments
 (0)