In order to keep up with modern demands, today's frameworks and applications require a flexible configuration system. Symfony fulfils this role nicely through its robust configuration files and environments concept.
The default Symfony configuration file config.yml is located under the app/config/ directory, with (partial) content sectioned as follows:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
framework:
…
# Twig Configuration
twig:
…
# Doctrine Configuration
doctrine:
…
# Swiftmailer Configuration
swiftmailer:
…The top-level entries like framework, twig, doctrine, and swiftmailer define the configuration of an individual bundle.
Optionally, the configuration file can be of XML or PHP format (config.xml or config.php). While YAML is simple and readable, XML is more powerful, whereas PHP is powerful but less readable.
We can use the console tool to dump the entire configuration as shown here:
php bin/console config:dump-reference FrameworkBundle
The preceding example lists the config file for core FrameworkBundle. We can use the same command to show possible configurations for any bundle that implements container extension, something we will look into later on.
Symfony has a nice implementation of environment concept. Looking into the app/config directory, we can see that default Symfony project actually starts with three different environments:
config_dev.ymlconfig_prod.ymlconfig_test.ymlEach application can run in various environments. Each environment shares the same code, but different configuration. Whereas dev environment might make use of extensive logging, a prod environment might make use of extensive caching.
The way these environments get triggered is via the front controller file, as in the following partial examples:
# web/app.php
…
$kernel = new AppKernel('prod', false);
…
# web/app_dev.php
…
$kernel = new AppKernel('dev', true);
…The test environment is missing here, as it is used only when running automated tests and cannot be accessed directly via a browser.
The app/AppKernel.php file is the one that actually loads the configuration, whether it is YAML, XML, or PHP as shown in the following code fragment:
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}The environments follow the same concept, whereas each environment imports the base configuration file and then modifies its values to suit the needs of the specific environment.