How to Use Matchers to Enable the Profiler Conditionally

How to Use Matchers to Enable the Profiler Conditionally

By default, the profiler is only activated in the development environment. But it's imaginable that a developer may want to see the profiler even in production. Another situation may be that you want to show the profiler only when an admin has logged in. You can enable the profiler in these situations by using matchers.

Using the built-in Matcher

Symfony provides a built-in matcher which can match paths and IPs. For example, if you want to only show the profiler when accessing the page with the 168.0.0.1 IP, then you can use this configuration:

  • YAML
    1
    2
    3
    4
    5
    6
    # app/config/config.yml
    framework:
        # ...
        profiler:
            matcher:
                ip: 168.0.0.1
    
  • XML
    1
    2
    3
    4
    5
    6
    <!-- app/config/config.xml -->
    <framework:config>
        <framework:profiler
            ip="168.0.0.1"
        />
    </framework:config>
    
  • PHP
    1
    2
    3
    4
    5
    6
    // app/config/config.php
    $container->loadFromExtension('framework', array(
        'profiler' => array(
            'ip' => '168.0.0.1',
        ),
    ));
    

You can also set a path option to define the path on which the profiler should be enabled. For instance, setting it to ^/admin/ will enable the profiler only for the /admin/ URLs.

Creating a custom Matcher

You can also create a custom matcher. This is a service that checks whether the profiler should be enabled or not. To create that service, create a class which implements RequestMatcherInterface. This interface requires one method: matches(). This method returns false to disable the profiler and true to enable the profiler.

To enable the profiler when a ROLE_SUPER_ADMIN is logged in, you can use something like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// src/Acme/DemoBundle/Profiler/SuperAdminMatcher.php
namespace Acme\DemoBundle\Profiler;

use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;

class SuperAdminMatcher implements RequestMatcherInterface
{
    protected $authorizationChecker;

    public function __construct(AuthorizationCheckerInterface $authorizationChecker)
    {
        $this->authorizationChecker = $authorizationChecker;
    }

    public function matches(Request $request)
    {
        return $this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN');
    }
}

2.6The AuthorizationCheckerInterface was introduced in Symfony 2.6. Prior, you had to use the isGranted method of SecurityContextInterface.

Then, you need to configure the service:

  • YAML
    1
    2
    3
    4
    services:
        acme_demo.profiler.matcher.super_admin:
            class: Acme\DemoBundle\Profiler\SuperAdminMatcher
            arguments: ["@security.authorization_checker"]
    
  • XML
    1
    2
    3
    4
    5
    <services>
        <service id="acme_demo.profiler.matcher.super_admin"
            class="Acme\DemoBundle\Profiler\SuperAdminMatcher">
            <argument type="service" id="security.authorization_checker" />
    </services>
    
  • PHP
    1
    2
    3
    4
    5
    6
    7
    use Symfony\Component\DependencyInjection\Definition;
    use Symfony\Component\DependencyInjection\Reference;
    
    $container->setDefinition('acme_demo.profiler.matcher.super_admin', new Definition(
        'Acme\DemoBundle\Profiler\SuperAdminMatcher',
        array(new Reference('security.authorization_checker'))
    );
    

2.6The security.authorization_checker service was introduced in Symfony 2.6. Prior to Symfony 2.6, you had to use the isGranted() method of the security.context service.

Now the service is registered, the only thing left to do is configure the profiler to use this service as the matcher:

  • YAML
    1
    2
    3
    4
    5
    6
    # app/config/config.yml
    framework:
        # ...
        profiler:
            matcher:
                service: acme_demo.profiler.matcher.super_admin
    
  • XML
    1
    2
    3
    4
    5
    6
    7
    <!-- app/config/config.xml -->
    <framework:config>
        <!-- ... -->
        <framework:profiler
            service="acme_demo.profiler.matcher.super_admin"
        />
    </framework:config>
    
  • PHP
    1
    2
    3
    4
    5
    6
    7
    // app/config/config.php
    $container->loadFromExtension('framework', array(
        // ...
        'profiler' => array(
            'service' => 'acme_demo.profiler.matcher.super_admin',
        ),
    ));
    

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License .