Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different security.yml files for different environments

Tags:

php

symfony

I could not manage to find a way to include different security.yml files that would be included depending on Symfony2`s environment. For example I wanted to have an in-memory user provider for my acceptance tests, cause I don't really need to test my entities and stuff here, I only want to make an acceptance test for my views.

But, as it turned out, it's not an easy thing to do. I removed security.yml from includes in my config.yml, renamed it to security_prod.yml and created a security_test.yml which has the in_memory user provider. Then I've included security_prod.yml and security_test.yml in my production and testing configs respectively.

Yet it does not seem to work at all:

$ 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.                                                                                                                              

It appeared to me like the security.yml filename was hardcoded (which would be way too weird for Symfony), and it wasn't.

So the question is: how do I get multiple security.ymls with Symfony? And what could be causing this behaviour?

like image 515
kix Avatar asked Aug 09 '12 08:08

kix


1 Answers

Instruction for those, who is looking for it (and do not red comments):

  1. Create different config files for different environments: config_test.yml, config_dev.yml, config_prod.yml
  2. Create different security files: security_test.yml, security_dev.yml, security_prod.yml
  3. Import security_test.yml in config_test.yml and so on for other environments. Example for config_test.yml:

    imports:
      - { resource: security_test.yml }
    
  4. Make sure you included security_*.yml only once (basically author did this mistake)
like image 133
pliashkou Avatar answered Sep 16 '22 18:09

pliashkou