本文介绍了Symfony2进入位于security.yml中的access_control参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将access_control参数作为我的自定义服务中的数组放在我的security.yml中。

就像获取role_hierarchy参数一样,我认为它可以与以下代码:


$ accessParameters = $ this->容器-> getParameter('security.access_control');


不幸的是事实并非如此。

有人可以告诉如何获取参数吗?

I'm trying to get the access_control parameters wich are located in my security.yml as an array in my custom service.
Just like with getting the role_hierarchy parametes I thought it would work with the following code:

$accessParameters = $this->container->getParameter('security.access_control');

Unfortunately this was not the case.
Can someone tell how to get the parameters?

推荐答案

无法从容器中获取 access_control 参数。

这是因为参数为稍后在,然后将其保留下来,而无需将其注册到容器中。

There's no way to get the access_control parameter from the container.
This is because this parameter is only used to create request matchers which will be registered as AccessMap later given in the AccessListener, and then are left over without registering it into the container.

您可以尝试一些骇人听闻的匹配器,使它们像这样

You can try something hacky to get these matchers back by getting them like

$context  = $this->get("security.firewall.map.context.main")->getContext();
$listener = $context[0][5];
// Do reflection on "map" private member

但这有点丑陋解决方案。

But this is kind of an ugly solution.

我能看到的另一种方法是再次解析安全文件

Another way I can see on how to get them is to parse again the security file

use Symfony\Component\Yaml\Yaml;

$file   = sprintf("%s/config/security.yml", $this->container->getParameter('kernel.root_dir'));
$parsed = Yaml::parse(file_get_contents($file));

$access = $parsed['security']['access_control'];

如果要将此配置注册到服务中,可以执行类似

If you want to register this configuration into a service, you can do something like

services:
    acme.config_provider:
        class: Acme\FooBundle\ConfigProvider
        arguments:
            - "%kernel.root_dir%"
    acme.my_service:
        class: Acme\FooBundle\MyService
        arguments:
            - "@acme.config_provider"
use Symfony\Component\Yaml\Yaml;

class ConfigProvider
{
    protected $rootDir;

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

    public function getConfiguration()
    {
        $file = sprintf(
            "%s/config/security.yml",
            $this->rootDir
        );
        $parsed = Yaml::parse(file_get_contents($file));

        return $parsed['security']['access_control'];
    }
}
class MyService
{
    protected $provider;

    public function __construct(ConfigProvider $provider)
    {
        $this->provider = $provider;
    }

    public function doAction()
    {
        $access = $this->provider->getConfiguration();

        foreach ($access as $line) {
            // ...
        }
    }
}

这篇关于Symfony2进入位于security.yml中的access_control参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:02