Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Cookbook
  4. Profiler
  5. How to use Matchers to enable the Profiler Conditionally
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Using the built-in Matcher
  • Creating a Custom Matcher

How to use Matchers to enable the Profiler Conditionally

Edit this page

Warning: You are browsing the documentation for Symfony 2.2, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

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

Symfony2 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
  • XML
  • PHP
1
2
3
4
5
6
# app/config/config.yml
framework:
    # ...
    profiler:
        matcher:
            ip: 168.0.0.1
1
2
3
4
5
6
<!-- app/config/config.xml -->
<framework:config>
    <framework:profiler
        ip="168.0.0.1"
    />
</framework:config>
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\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;

class SuperAdminMatcher implements RequestMatcherInterface
{
    protected $securityContext;

    public function __construct(SecurityContext $securityContext)
    {
        $this->securityContext = $securityContext;
    }

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

Then, you need to configure the service:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
parameters:
    acme_demo.profiler.matcher.super_admin.class: Acme\DemoBundle\Profiler\SuperAdminMatcher

services:
    acme_demo.profiler.matcher.super_admin:
        class: "%acme_demo.profiler.matcher.super_admin.class%"
        arguments: ["@security.context"]
1
2
3
4
5
6
7
8
9
10
11
<parameters>
    <parameter
        key="acme_demo.profiler.matcher.super_admin.class"
    >Acme\DemoBundle\Profiler\SuperAdminMatcher</parameter>
</parameters>

<services>
    <service id="acme_demo.profiler.matcher.super_admin"
        class="%acme_demo.profiler.matcher.super_admin.class%">
        <argument type="service" id="security.context" />
</services>
1
2
3
4
5
6
7
8
9
10
11
12
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$container->setParameter(
    'acme_demo.profiler.matcher.super_admin.class',
    'Acme\DemoBundle\Profiler\SuperAdminMatcher'
);

$container->setDefinition('acme_demo.profiler.matcher.super_admin', new Definition(
    '%acme_demo.profiler.matcher.super_admin.class%',
    array(new Reference('security.context'))
);

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

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
# app/config/config.yml
framework:
    # ...
    profiler:
        matcher:
            service: acme_demo.profiler.matcher.super_admin
1
2
3
4
5
6
<!-- app/config/config.xml -->
<framework:config>
    <framework:profiler
        service="acme_demo.profiler.matcher.super_admin"
    />
</framework:config>
1
2
3
4
5
6
// app/config/config.php
$container->loadFromExtension('framework', array(
    'profiler' => array(
        'service' => 'acme_demo.profiler.matcher.super_admin',
    ),
));
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Show your Sylius expertise

Show your Sylius expertise

Measure & Improve Symfony Code Performance

Measure & Improve Symfony Code Performance

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of William Pinaud (DocFX), a Symfony contributor

Thanks William Pinaud (DocFX) for being a Symfony contributor

1 commit • 2 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia