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

TWITTER

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 in  Uncategorized   |     |  delicious  Digg

5 Responses to “Symfony: returning an array from app.yml”

  1. 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.

    By Russ on Jul 13, 2009

  2. 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??

    By chris on Jul 13, 2009

  3. My friend Lawrence discusses this a bit more in depth on his blog. http://www.teamlalala.com/blog/2009/09/20/how-to-get-an-array-out-of-a-yaml-file-using-symfonys-parser/

    By Scott Meves on Sep 24, 2009

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

    By Rodrigo on Dec 11, 2009

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

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

    By sglessard on Feb 24, 2010

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word