Assetic Bundles 缓存 Composer 配置 控制台 控制器 调试 部署 Doctrine 电子邮件 事件分发器 表达式 表单 前端 日志 分析器 请求 路由 安全 序列化 服务容器 会话 PSR 7 Symfony 版本 模板 测试 升级 验证 Web 服务器 Web 服务 工作流

发布于 2015-12-06 07:22:56 | 169 次阅读 | 评论: 0 | 来源: 网络整理

默认情况下,分析器只在开发环境中被启用。但可以想象的是作为一个开发商想要看到分析器即使是在产品中。另一种情况可能是,仅当一个管理员登录时,你想要启用分析器。你可以通过使用匹配器在这些情况下实现分析器的启用。

使用内置的匹配

Symfony 提供了一个内置的匹配器用来匹配路径和 IPS 。例如,如果你需要仅当 168.0.0.1 IP 访问网页时显示分析器,那么你可以使用这个配置:

YAML:

# app/config/config.yml
framework:
    # ...
    profiler:
        matcher:
            ip: 168.0.0.1

XML:

<!-- app/config/config.xml -->
<framework:config>
    <framework:profiler
        ip="168.0.0.1"
    />
</framework:config>

PHP:

// app/config/config.php
$container->loadFromExtension('framework', array(
    'profiler' => array(
        'ip' => '168.0.0.1',
    ),
));  

还可以设置路径选项来定义应启用分析器的路径。 例如,设置它为 ^/admin/ 将使仅为 ^/admin/ 网址来启用分析器。

创建一个自定义匹配器

你也可以创建一个自定义的匹配器。这是一个检查是否启用或不启用分析器的服务。创建这个服务,创建一个类实现 requestmatcherinterface 接口。这个接口需要一个方法:matches()。 此方法返回假以禁用事件分析器和返回真来启用分析器。

// src/AppBundle/Profiler/SuperAdminMatcher.php
namespace AppBundle\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');
    }
}

在 Symfony 2.6 中介绍了 AuthorizationCheckerInterface 。之前,你必须使用 SecurityContextInterface 中的 isGranted 方法。

然后,您需要配置服务:

YAML:

# app/config/services.yml
services:
    app.profiler.matcher.super_admin:
        class: AppBundle\Profiler\SuperAdminMatcher
        arguments: ["@security.authorization_checker"]

XML:

<!-- app/config/services.xml -->
<services>
    <service id="app.profiler.matcher.super_admin"
        class="AppBundle\Profiler\SuperAdminMatcher">
        <argument type="service" id="security.authorization_checker" />
</services>

PHP:

// app/config/services.php
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$container->setDefinition('app.profiler.matcher.super_admin', new Definition(
    'AppBundle\Profiler\SuperAdminMatcher',
    array(new Reference('security.authorization_checker'))
);

Symfony 2.6 中介绍了安全认证检查服务(security.authorization_checker service) ,你必须使用 security.context 服务中的 isGranted() 方法。

现在服务已经被注册,唯一剩下要做的就是配置分析器使用该服务作为匹配器:

YAML:

# app/config/config.yml
framework:
    # ...
    profiler:
        matcher:
            service: app.profiler.matcher.super_admin

XML:

<!-- app/config/config.xml -->
<framework:config>
    <!-- ... -->
    <framework:profiler
        service="app.profiler.matcher.super_admin"
    />
</framework:config>

PHP:

// app/config/config.php
$container->loadFromExtension('framework', array(
    // ...
    'profiler' => array(
        'service' => 'app.profiler.matcher.super_admin',
    ),
));
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务