Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Components
  4. Dependency Injection
  5. Introduction to Parameters
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Getting and Setting Container Parameters
  • Parameters in Configuration Files
  • Array Parameters
  • Constants as Parameters
  • PHP Keywords in XML

Introduction to Parameters

Edit this page

Warning: You are browsing the documentation for Symfony 2.4, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

Introduction to Parameters

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.

Getting and Setting Container Parameters

Working with container parameters is straightforward using the container's accessor methods for parameters. You can check if a parameter has been defined in the container with:

1
$container->hasParameter('mailer.transport');

You can retrieve a parameter set in the container with:

1
$container->getParameter('mailer.transport');

and set a parameter in the container with:

1
$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.

Parameters in Configuration Files

You can also use the parameters section of a config file to set parameters:

1
2
parameters:
    mailer.transport: sendmail
1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="mailer.transport">sendmail</parameter>
    </parameters>
</container>
1
$container->setParameter('mailer.transport', 'sendmail');

As well as retrieving the parameter values directly from the container you can use them in the config files. You can refer to parameters elsewhere 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:
    mailer:
        class:     Mailer
        arguments: ['%mailer.transport%']
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="mailer.transport">sendmail</parameter>
    </parameters>

    <services>
        <service id="mailer" class="Mailer">
            <argument>%mailer.transport%</argument>
        </service>
    </services>
</container>
1
2
3
4
5
6
7
use Symfony\Component\DependencyInjection\Reference;

$container->setParameter('mailer.transport', 'sendmail');

$container
    ->register('mailer', 'Mailer')
    ->addArgument('%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>

If you were using this elsewhere as well, then you would only need to change the parameter value in one place if needed.

You can also use the parameters in the service definition, for example, making the class of a service a parameter:

1
2
3
4
5
6
7
8
parameters:
    mailer.transport: sendmail
    mailer.class: Mailer

services:
    mailer:
        class:     "%mailer.class%"
        arguments: ["%mailer.transport%"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="mailer.transport">sendmail</parameter>
        <parameter key="mailer.class">Mailer</parameter>
    </parameters>

    <services>
        <service id="mailer" class="%mailer.class%">
            <argument>%mailer.transport%</argument>
        </service>
    </services>
</container>
1
2
3
4
5
6
7
8
use Symfony\Component\DependencyInjection\Reference;

$container->setParameter('mailer.transport', 'sendmail');
$container->setParameter('mailer.class', 'Mailer');

$container
    ->register('mailer', '%mailer.class%')
    ->addArgument('%mailer.transport%');

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"]
1
<argument>http://symfony.com/?foo=%%s&bar=%%d</argument>
1
->addArgument('http://symfony.com/?foo=%%s&bar=%%d');

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
11
12
parameters:
    my_mailer.gateways:
        - mail1
        - mail2
        - mail3
    my_multilang.language_fallback:
        en:
            - en
            - fr
        fr:
            - fr
            - en
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="my_mailer.gateways" type="collection">
            <parameter>mail1</parameter>
            <parameter>mail2</parameter>
            <parameter>mail3</parameter>
        </parameter>
        <parameter key="my_multilang.language_fallback" type="collection">
            <parameter key="en" type="collection">
                <parameter>en</parameter>
                <parameter>fr</parameter>
            </parameter>
            <parameter key="fr" type="collection">
                <parameter>fr</parameter>
                <parameter>en</parameter>
            </parameter>
        </parameter>
    </parameters>
</container>
1
2
3
4
5
$container->setParameter('my_mailer.gateways', array('mail1', 'mail2', 'mail3'));
$container->setParameter('my_multilang.language_fallback', array(
    'en' => array('en', 'fr'),
    'fr' => array('fr', 'en'),
));

Constants as Parameters

The container also has 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
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="global.constant.value" type="constant">GLOBAL_CONSTANT</parameter>
        <parameter key="my_class.constant.value" type="constant">My_Class::CONSTANT_NAME</parameter>
    </parameters>
</container>
1
2
$container->setParameter('global.constant.value', GLOBAL_CONSTANT);
$container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME);

Note

This does not work for YAML configurations. If you're using YAML, you can import an XML file to take advantage of this functionality:

1
2
imports:
    - { resource: parameters.xml }

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.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Janusz Slota, a Symfony contributor

    Thanks Janusz Slota (@janusz.slota) for being a Symfony contributor

    3 commits • 6 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch