我可以在Symfony中导入参数定义的文件吗?

时间:2012-12-10 13:09:37

标签: php symfony

对于特定情况,我的Symfony2项目具有不同版本的配置文件。一个具体的简单示例是安全文件:

security_db.yml
security_ldap.yml

我希望在parameters.ini文件中定义使用过的文件。 这是我尝试在config.yml中添加的内容:

imports:
- { resource: "security_%authentification%.yml" }

虽然我的authentification文件中定义了parameters.ini参数,但我总是遇到同样的错误:

The file "security_%authentification%.yml" does not exist

1 个答案:

答案 0 :(得分:1)

我相信一种方法是使用Extension类根据parameters.ini中定义的内容加载正确的配置...这是未经测试的......

<强>的src / MyAwesomeBundle / DependencyInjection / MyAwesomeExtension.php

class MyAwesomeExtension extends Extension 
{

    public function load(array $configs, ContainerBuilder $container)
    {
       // boiler plate code here ... this probably already exists in your class.
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');

        // your personalized loader goes here ...
        $myloader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

        $auth = $container->getParameter('authentication');
        if (isset($auth)) {
            $myloader->load('security_' . $auth . '.yml');
        }
    }

}