Skip to content

Instantly share code, notes, and snippets.

@wouterj
Last active August 29, 2015 14:16
Show Gist options
  • Save wouterj/27ffbdac34d91f87180b to your computer and use it in GitHub Desktop.
Save wouterj/27ffbdac34d91f87180b to your computer and use it in GitHub Desktop.
The confusing world of Symfony DI configuration files

It all starts with the booting of the kernel, which builds the container. As you see, $this->registerContainerConfiguration() is called (an abstract method)

class Kernel
{
    protected function buildContainer()
    {
        $container = $this->getContainerBuilder();
        $container->addObjectResource($this);
        $this->prepareContainer($container);
        if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
            $container->merge($cont);
        }
    }
}

https://github.com/symfony/HttpKernel/blob/master/Kernel.php#L577-601

This method is set in the AppKernel:

public function registerContainerConfiguration(LoaderInterface $loader)
{
    $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}

https://github.com/symfony/symfony-standard/blob/2.7/app/AppKernel.php#L32-35

Assume it's the prod environment, $this->getRootDir()/config/config_prod.yml is loaded:

# app/config/config_prod.yml
imports:
    - { resource: config.yml }
    
# ...

As you can see, it imports config.yml:

# app/config/config.yml
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

# ...

As you see here, config.yml imports the parameters.yml.

All these files are loaded by the same loaders and there is no difference in the handling of these files. So config.yml can define parameters:

# app/config/config.yml

# ...

parameters:
    locale: en

And parameters.yml can configure extensions:

# app/config/parameters.yml
framework:
    secret: oihpoaehp97235rgio

However, I don't recommend to do this, as all files have different meanings:

  • config.yml/config_*.yml define extension configuration
  • parameters.yml defines environment parameters, that shouldn't be commited
  • services.yml configures global service container parameters and services
  • security.yml defines security configuration (which is just an extension), as it can get quite complictated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment