Home » Php » php – Symfony 2 YAML passing arrays

php – Symfony 2 YAML passing arrays

Posted by: admin July 12, 2020 Leave a comment

Questions:

I wonder where can I get more information about special syntax like @somevar or %somevar% in symfony2’s yaml configuration?

For example, using @ defines a call to a service, that is how we pass dependencies to services. %somevar% on the other hand refers to the value of an already defined parameter with the name somevar.

So, if I do:

parameters:
    custom: "some value"
    another: %custom%

then another will be populated with the value of custom, which in my case is “some value”. My question is, where are these relations documented?

My particular need is to be able to reference an element of an array, something like %somevar[somekey]%, but that syntax doesn’t work.

Thanks in advance!

EDIT: I found this: Full merge key support.
Full support for references, aliases, and full merge key. Don't repeat yourself by referencing common configuration bits.

in the yaml docs, but no furthur documentation about it..

How to&Answers:

What you are searching for is not really about Yaml itself, but about the Yaml loader of the Dependency Injection container.

If you search docs about it, here are the ones for the old component (v1): http://components.symfony-project.org/dependency-injection/trunk/book/05-Service-Description

Symfony2 comes with a new component (based on the same principles). You can find the official docs here: http://symfony.com/doc/current/book/service_container.html#service-parameters

Concerning your problem, you cannot access to keys of DI parameters, you have to flatten then manually.

You could use a DI extension to fit your need, take example on some bundles like: https://github.com/symfony/AsseticBundle/blob/master/DependencyInjection/AsseticExtension.php#L54 (maybe not the best example).