2012-12-10 43 views
0

對於一個特定的情況,我的Symfony2項目有不同版本的配置文件。一個具體的簡單的例子是安全文件:我可以在Symfony中導入參數定義文件嗎?

security_db.yml 
security_ldap.yml 

我想在parameters.ini文件中定義了使用的文件。 這是我試圖在添加config.yml

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

雖然有在我parameters.ini文件中定義的authentification參數,但我一直有同樣的錯誤:

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

回答

1

我相信這樣做的一種方式這是使用你的Extension類來根據你的parameters.ini中定義的內容加載適當的配置...這是未經測試的...

src/MyAwesomeBundle/Dep endencyInjection/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'); 
     } 
    } 

} 
相關問題