适用于不同环境的不同 security.yml 文件


Different security.yml files for different environments

我无法找到一种方法来包含不同的security.yml文件,这些文件将根据Symfony2的环境而包含。例如,我想为我的验收测试提供一个内存中的用户提供程序,因为我真的不需要在这里测试我的实体和东西,我只想对我的视图进行验收测试。

但是,事实证明,这不是一件容易的事情。我从config.yml的包含中删除了security.yml,将其重命名为security_prod.yml并创建了一个具有in_memory用户提供程序的security_test.yml。然后,我分别在生产和测试配置中包含security_prod.ymlsecurity_test.yml

然而,它似乎根本不起作用:

$ SYMFONY_ENV=test app/console cache:clear                                                      
  [Symfony'Component'Config'Definition'Exception'InvalidConfigurationException]                                                           
  You are not allowed to define new elements for path "security.providers". Please define all elements for this path in one config file.  
$ SYMFONY_ENV=prod app/console cache:clear                                                      
  [Symfony'Component'Config'Definition'Exception'ForbiddenOverwriteException]                                                                         
  Configuration path "security.access_control" cannot be overwritten. You have to define all options for this path, and any of its sub-paths in one   
  configuration section.                                                                                                                              

在我看来,security.yml文件名是硬编码的(这对Symfony来说太奇怪了),但事实并非如此。

所以问题是:我如何使用Symfony获得多个security.yml?是什么导致了这种行为?

给那些正在寻找它的人的说明(不要红色评论):

  1. 为不同的环境创建不同的config文件:config_test.ymlconfig_dev.ymlconfig_prod.yml
  2. 创建不同的security文件:security_test.ymlsecurity_dev.ymlsecurity_prod.yml
  3. 导入security_test.yml config_test.yml等其他环境。config_test.yml示例:

    imports:
      - { resource: security_test.yml }
    
  4. 确保你只包含security_*.yml一次(基本上作者犯了这个错误)