SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

July 13, 2009 – 10:37am Symfony: returning an array from app.yml

Sometimes its useful to return an array of values from app.yml. This is buried in the symfony docs so I thought I’d post it here just in case it’d help.

I’ve been using this technique to store a list of languages available on a site. As translations are ready for other languages, I add them to the list here and they automatically get pulled into the code in the appropriate places:

  .array:
    languages: 
      en_US: English
      he_IL: Hebrew

This allows you to retrieve the value of your “languages” parameter as an array:

$languages = sfConfig::get('app_languages'); 
print_r($languages);
/*
output:
Array
(
    [en_US] => English
    [he_IL] => Hebrew
)
*/

If you didn’t use the “.array” syntax, you would have to retrieve each language like this:

// in app.yml
languages: 
  en_US: English
  he_IL: Hebrew
$languages = sfConfig::get('app_languages'); // returns NULL
$english = sfConfig::get('app_languages_en_US'); // returns English

You can read more about this in the symfony book. Can anyone verify this works in the latest version of symfony?

Posted by in  Uncategorized   |  

7 Responses to Symfony: returning an array from app.yml

  1. Russ says:

    Yes, it’s valid syntax so will be supported indefinitely as there would be no reason to remove it. The “dot notation” defines a header, and anything below that is treated as an array.

    It does not need to be .array, but can be .anything, however this is purely for the yaml file, as whatever word you use is ignored when the yaml file is parsed into a php array.

  2. chris says:

    actually in the app.yml the “dotnotation” is not just a header as described in the docs and used in other areas e.g settings.yml, I just tested it now.

    It wouldnt print an array unless i included the .header
    …is this a bug in the sfconfig::get function??

  3. Rodrigo says:

    Yes, it’s valid sintax in symfony 1.3/1.4

  4. sglessard says:

    I just noticed app.yml can only have one .array declaration (to confirm):

    .array
      var:
        foo: bar
        fooa: bara
      var2:
        foo2: bar2

  5. kaxior says:

    Thanks a lot, i`ve been looking for it almost an hour 😉

  6. Vertigo says:

    Brilliant – exactly what I needed. Thank you.