0
votes

If i use in Symfony2 /app/config/config.yml:

parameters:
    test_first: aaaa
    test_second: bbbb

twig:
    globals:
        first: %test_first%

then this working ok

but i would like make:

parameters:
    test:
        first: aaaa
        second: bbbb

twig:
    globals:
        first: %test_first%

i have error:

ParameterNotFoundException: You have requested a non-existent parameter "test_first".

How can i make it?

1
The parameters.yml values maps against the service_container's parameter collection, which are given flat keys. What you have created is a parameter called 'test' which is an associative array. - Flosculus
so how can i make it? - maurowallace
Does %test.first% work ? - Sethunath K M
%test.first% not working - maurowallace

1 Answers

2
votes

Dependency Injection is a lengthy subject in Symfony and there is plenty of documentation on the details, so I'll just give you the run down here.

The parameters.yml file should contain flat key/value pairs. Config files should read from them, it is possible for parameters to contain arrays, however you cannot reference the subs in Symfony like this.

Instead you can give a config node the entire parameter array. Which takes you to: http://symfony.com/doc/current/components/config/definition.html

Next step is to alter the Service Container in the Extension class of your bundle. http://symfony.com/doc/current/components/dependency_injection/compilation.html#managing-configuration-with-extensions

Read from the config node which contains the parameters from the yml and create a superset of container parameters.

For example:

foreach($config['test'] as $key => $value) {
    $container->setParameter($this->getAlias().'.test.'.$key, $value);
}

This will create a section of parameters such as:

myapp.test.first  = aaaa
myapp.test.second = bbbb

And to access from a controller:

$value = $this->container->getParameter('myapp.test.first');

Be aware however that bundles should not really be dependent on the existence of parameters.yml values, because if that parameter does not exist, the above statement will throw an error, unless you call hasParameter first.