Introduction to Parameters
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.2 (the current stable version).
You can define parameters in the service container which can then be used directly or as part of service definitions. This can help to separate out values that you will want to change more regularly.
Parameters in Configuration Files
Use the parameters
section of a config file to set parameters:
1 2
parameters:
mailer.transport: sendmail
You can refer to parameters elsewhere in any config file by surrounding them
with percent (%
) signs, e.g. %mailer.transport%
. One use for this is
to inject the values into your services. This allows you to configure different
versions of services between applications or multiple services based on the
same class but configured differently within a single application. You could
inject the choice of mail transport into the Mailer
class directly. But
declaring it as a parameter makes it easier to change rather than being tied up
and hidden with the service definition:
1 2 3 4 5 6 7
parameters:
mailer.transport: sendmail
services:
app.mailer:
class: AppBundle\Mailer
arguments: ['%mailer.transport%']
Caution
The values between parameter
tags in XML configuration files are
not trimmed.
This means that the following configuration sample will have the value
\n sendmail\n
:
1 2 3
<parameter key="mailer.transport">
sendmail
</parameter>
In some cases (for constants or class names), this could throw errors. In order to prevent this, you must always inline your parameters as follow:
1
<parameter key="mailer.transport">sendmail</parameter>
Note
The percent sign inside a parameter or argument, as part of the string, must be escaped with another percent sign:
1
arguments: ['http://symfony.com/?foo=%%s&bar=%%d']
Getting and Setting Container Parameters in PHP
Working with container parameters is straightforward using the container's accessor methods for parameters:
1 2 3 4 5 6 7 8
// checks if a parameter is defined
$container->hasParameter('mailer.transport');
// gets value of a parameter
$container->getParameter('mailer.transport');
// adds a new parameter
$container->setParameter('mailer.transport', 'sendmail');
Caution
The used .
notation is just a
Symfony convention to make parameters
easier to read. Parameters are just flat key-value elements, they can't
be organized into a nested array
Note
You can only set a parameter before the container is compiled. To learn more about compiling the container see Compiling the Container.
Array Parameters
Parameters do not need to be flat strings, they can also contain array values.
For the XML format, you need to use the type="collection"
attribute
for all parameters that are arrays.
1 2 3 4 5 6 7 8 9 10
parameters:
my_mailer.gateways: [mail1, mail2, mail3]
my_multilang.language_fallback:
en:
- en
- fr
fr:
- fr
- en
Constants as Parameters
The XML and PHP formats also have support for setting PHP constants as parameters.
To take advantage of this feature, map the name of your constant to a parameter
key and define the type as constant
.
1 2 3
parameters:
global.constant.value: "@=constant('GLOBAL_CONSTANT')"
my_class.constant.value: "@=constant('My_Class::CONSTANT_NAME')"
Caution
YAML files can refer to PHP constants via the @=constant('CONSTANT_NAME')
syntax, which is provided by the
Expression Language component. See
The Expression Syntax to learn more about its syntax.
Tip
If you're using YAML, you can import an XML file to take advantage of this functionality:
1 2
imports:
- { resource: parameters.xml }
Note
In Symfony 3.2, YAML supports PHP constants via the !php/const:CONSTANT_NAME
syntax.
PHP Keywords in XML
By default, true
, false
and null
in XML are converted to the
PHP keywords (respectively true
, false
and null
):
1 2 3 4 5 6 7
<parameters>
<parameter key="mailer.send_all_in_once">false</parameter>
</parameters>
<!-- after parsing
$container->getParameter('mailer.send_all_in_once'); // returns false
-->
To disable this behavior, use the string
type:
1 2 3 4 5 6 7
<parameters>
<parameter key="mailer.some_parameter" type="string">true</parameter>
</parameters>
<!-- after parsing
$container->getParameter('mailer.some_parameter'); // returns "true"
-->
Note
This is not available for YAML and PHP, because they already have built-in support for the PHP keywords.