Symfony2 获取位于 security.yml 中的 access_control 参数 [英] Symfony2 get to the access_control parameters located in the security.yml
问题描述
我正在尝试获取位于我的 security.yml 中的 access_control 参数作为我的自定义服务中的数组.
I'm trying to get the access_control parameters which are located in my security.yml as an array in my custom service.
就像获取 role_hierarchy 参数一样,我认为它适用于以下代码:
Just like with getting the role_hierarchy parameters 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
参数.
这是因为这个参数是 only用于创建请求匹配器 将注册为 AccessMap 稍后在 AccessListener,然后在没有将其注册到容器中的情况下被遗留下来.
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 SymfonyComponentYamlYaml;
$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.yml
services:
acme.config_provider:
class: AcmeFooBundleConfigProvider
arguments:
- "%kernel.root_dir%"
acme.my_service:
class: AcmeFooBundleMyService
arguments:
- "@acme.config_provider"
AcmeFooBundleConfigProvider
use SymfonyComponentYamlYaml;
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'];
}
}
AcmeFooBundleMyService
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 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!