Symfony: Depth of configuration files

October 15, 2009

The symfony framework gives you the possibility to configure nearly anything in your application with YAML files. You can read more about the configuration system in the official symfony guide.

When you have a first look at these configuration files especially on examples for ‘app.yml’, you may think that you can structure the file in any way you want i.e. the amount of levels. Well I noticed, that this is not the case.

To make it short: You can set up any number of levels but only the second and the third level are directly accessible via sfConfig::get().

The longer version:

My configuration file looked like this:

all:
  .settings:
    some_parameter: value

  somePluginSettings:
    parameter: value

dev:
  email:
    addresses:
      address_1: foo@bar.tld
    smtp:
      host: smtp.host.tld
      config:
        username: username
        password: password

I was able to access every parameter via sfConfig::get() except those below the email key. An invocation of sfConfig::get(‘app_email_smtp_host’) delivered NULL.

The solution is rather simple: Symfony parses every key up to level 3 i.e. in this example up to the key ‘smtp’. Everything below level 3 is parsed as an array. So  sfConfig::get(‘app_email_smtp’) delivers an array:

array(2) { ["host"]=> string(19)"smtp.host.tld" ["config"]=> array(2) { ["username"]=> string(19) "username"  ["password"]=> string(9) "password" } }

And it makes no differences if you use a ‘normal’ key like ‘smtp’ or if you use what I would call a ‘hidden’ key like ‘.smtp’ (see the dot).

So for the record: Each default configuration file can consist of up to 3 levels:

  • Level 1: Defines the environment like production, development. This is set by the dispatcher.
  • Level 2: Defines either a group of settings or atomic values like strings and numbers. If it defines a group it can be a ‘normal’ or a ‘hidden’ key.
  • Level 3: Defines an valid value in the YAML syntax. Can be strings, numbers, arrays, etc.

Unfortunately I haven’t found any official description of this but maybe I read the documentation too quickly 😉 If you know an official source, please tell me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: