我正在尝试获取位于我的 security.yml 中的 access_control 参数作为自定义服务中的数组。
就像获取 role_hierarchy 参数一样,我认为它可以使用以下代码:
$accessParameters = $this->container->getParameter('security.access_control');
不幸的是,情况并非如此。谁能告诉我如何获取参数?
最佳答案
无法从容器中获取access_control
参数。
这是因为这个参数是only used创建request matchers将被注册为 AccessMap稍后在 AccessListener 中给出,然后被遗留下来而没有将其注册到容器中。
您可以尝试一些骇人听闻的方法来让这些匹配器恢复正常
$context = $this->get("security.firewall.map.context.main")->getContext();
$listener = $context[0][5];
// Do reflection on "map" private member
但这是一种丑陋的解决方案。
我可以看到如何获取它们的另一种方法是再次解析安全文件
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'];
如果你想将这个配置注册到一个服务中,你可以这样做
services.yml
services:
acme.config_provider:
class: Acme\FooBundle\ConfigProvider
arguments:
- "%kernel.root_dir%"
acme.my_service:
class: Acme\FooBundle\MyService
arguments:
- "@acme.config_provider"
Acme\FooBundle\ConfigProvider
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'];
}
}
Acme\FooBundle\MyService
class MyService
{
protected $provider;
public function __construct(ConfigProvider $provider)
{
$this->provider = $provider;
}
public function doAction()
{
$access = $this->provider->getConfiguration();
foreach ($access as $line) {
// ...
}
}
}
关于php - Symfony2 获取位于 security.yml 中的 access_control 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19831114/