Symfony2 arrays as parameters in xml dependeny injection

18.10.2011
Symfony documentation recommends to use XML for dependency injection configuration. I like the yml format because its simple and intuitive, but the main benefit of XML is the possibility for better validation and editing help.
So today we tried to port some configuration from yaml to xml.
Our yaml file contained an array parameter:

parameters:
symfony_cmf_multilang_content.lang_preference:
en: [en, de]
de: [de, en]

To make this work in XML, you need to nest parameter tags and use the type="collection" attribute.

<parameters>
<parameter key="symfony_cmf_multilang_content.lang_preference" type="collection">
<parameter key="en" type="collection">
<parameter>en</parameter> <parameter>de</parameter>
</parameter>
<parameter key="de" type="collection">
<parameter>de</parameter>
<parameter>en</parameter>
</parameter>
</parameter>
</parameters>

If you forget the type="collection" attribute, your parameter will contain whitespace from between the xml tags, but no content. You will probably just get some random error somewhere about things not being an array. (That is, if you do not validate your configuration. If you do, you will get errors in the validation so at least you will be closer to the place of the problem.)

symfony dic