Different security.yml files for different environments

php, symfony

Solution

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

- Create different `config` files for different environments: `config_test.yml`, `config_dev.yml`, `config_prod.yml`

- Create different `security` files: `security_test.yml`, `security_dev.yml`, `security_prod.yml`

Import `security_test.yml` in `config_test.yml` and so on for other environments. Example for `config_test.yml`:

imports:
  - { resource: security_test.yml }

- Make sure you included `security_*.yml` only once (basically author did this mistake)

Problem

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.yml`s with Symfony? And what could be causing this behaviour?

Original source