0

针对特定情况,我的 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
4

1 回答 1

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');
        }
    }

}
于 2012-12-10T14:10:13.077 回答