You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

How to Use Matchers to Enable the Profiler Conditionally

4.0 version

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
    1
    2
    3
    # config/services.yaml
    services:
        Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <!-- config/services.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services
            http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <service id="Symfony\Component\HttpKernel\Profiler\Profiler" alias="profiler" />
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    // config/services.php
    use Symfony\Component\HttpKernel\Profiler\Profiler;
    
    $container->setAlias(Profiler::class, 'profiler');
    

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