How to Use Matchers to Enable the Profiler Conditionally

Version: 4.0
Edit this page

Warning: You are browsing the documentation for version 4.0 which is not maintained anymore.

If some of your projects are still using this version, consider upgrading.

How to Use Matchers to Enable the Profiler Conditionally

Caution

The possibility to use a matcher to enable the profiler conditionally was removed in Symfony 4.0.

Symfony Profiler cannot be enabled/disabled conditionally using matchers, because that feature was removed in Symfony 4.0. However, you can use the enable() and disable() methods of the Profiler class in your controllers to manage the profiler programmatically:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use Symfony\Component\HttpKernel\Profiler\Profiler;
// ...

class DefaultController
{
    // ...

    public function someMethod(Profiler $profiler)
    {
        // for this particular controller action, the profiler is disabled
        $profiler->disable();

        // ...
    }
}

In order for the profiler to be injected into your controller you need to create an alias pointing to the existing profiler service:

  • YAML
  • XML
  • PHP
1
2
3
# config/services.yaml
services:
    Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.