How to get and use an associative array from YAML to action in Symfony?

I got some configuration data in app.yml and I want to foresee them in action. I try to do this by getting them sfConfig :: get ('app_datas'), but it does not work. Let's show them in detail:

YAML:

all: datas: foo: bar foo2: bar2 

and in actions.class.php I'm trying to use this code:

 foreach (sfConfig::get('app_datas') as $key => $value) { echo "key $key has value $value"; } 

it does not work because sfConfig :: get ('app_datas') is NULL, how easy is it to get it?

+6
arrays yaml symfony1 associative
source share
2 answers

When Symfony loads the app.yml configuration app.yml , it saves only Level 2. Therefore, you cannot directly access app_datas . If you want to get an array containing foo and foo2 , create a YAML file, for example:

 all: datas: baz: foo: bar foo2: bar2 

Then you can do sfConfig::get('app_datas_baz') , which will be an array containing foo and foo2 as keys.

In the editor : kuba way is better than a mannequin; forgot you can do it.

+9
source share

If you want to access the first level as an array, you can enter an intermediate level between them, as suggested by @jeremy. Prefix it with a dot if you do not want it to actually appear in config with variable names:

 all: .baz: datas: foo: bar foo2: bar2 

You should now have access to your data with:

 foreach (sfConfig::get('app_datas') as $key => $value) { echo "key $key has value $value"; } 
+15
source share

All Articles