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.

Obteniendo y estableciendo contenedores de parámetros

Working with container parameters is straightforward using the container’s accessor methods for parameters. Puedes comprobar si se ha definido un parámetro en el contenedor con:

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

You can retrieve a parameter set in the container with:

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

y establecer un parámetro en el contenedor con:

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

Nota

You can only set a parameter before the container is compiled. To learn more about compiling the container see Compilando el contenedor.

Parameters in Configuration Files

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

  • YAML
    parameters:
        mailer.transport: sendmail
    
  • XML
    <parameters>
        <parameter key="mailer.transport">sendmail</parameter>
    </parameters>
    
  • PHP
    $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 by making it a parameter. This makes it easier to change rather than being tied up and hidden with the service definition:

  • YAML
    parameters:
        mailer.transport: sendmail
    
    services:
        mailer:
            class:     Mailer
            arguments: ['%mailer.transport%']
    
  • XML
    <parameters>
        <parameter key="mailer.transport">sendmail</parameter>
    </parameters>
    
        <services>
        <service id="mailer" class="Mailer">
            <argument>%mailer.transport%</argument>
        </service>
        </services>
    
  • PHP
    use Symfony\Component\DependencyInjection\Reference;
    
    // ...
    $container->setParameter('mailer.transport', 'sendmail');
    $container
        ->register('mailer', 'Mailer')
        ->addArgument('%mailer.transport%');
    

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:

  • YAML
    parameters:
        mailer.transport: sendmail
        mailer.class: Mailer
    
    services:
        mailer:
            class:     '%mailer.class%'
            arguments: ['%mailer.transport%']
    
  • XML
    <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>
    
  • PHP
    use Symfony\Component\DependencyInjection\Reference;
    
    // ...
    $container->setParameter('mailer.transport', 'sendmail');
    $container->setParameter('mailer.class', 'Mailer');
    $container
        ->register('mailer', '%mailer.class%')
        ->addArgument('%mailer.transport%');
    
    $container
        ->register('newsletter_manager', 'NewsletterManager')
        ->addMethodCall('setMailer', array(new Reference('mailer')));
    

Nota

El signo de porcentaje en un parámetro o argumento, como parte de la cadena, se debe escapar con otro signo de porcentaje:

  • YAML
    arguments: ['http://symfony.com/?foo=%%s&bar=%%d']
    
  • XML
    <argument type="string">http://symfony.com/?foo=%%s&bar=%%d</argument>
  • PHP
    ->addArgument('http://symfony.com/?foo=%%s&bar=%%d');
    

Array Parameters

Los parámetros no tienen que ser cadenas planas, sino que también pueden ser arreglos. For the XML format, you need to use the type="collection" attribute for all parameters that are arrays.

  • YAML
    # app/config/config.yml
    parameters:
        my_mailer.gateways:
            - mail1
            - mail2
            - mail3
        my_multilang.language_fallback:
            en:
                - en
                - fr
            fr:
                - fr
                - en
    
  • XML
    <!-- app/config/config.xml -->
    <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>
    
  • PHP
    // app/config/config.php
    use Symfony\Component\DependencyInjection\Definition;
    
    $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

El contenedor también cuenta con apoyo para fijar constantes PHP como parámetros. Para aprovechar esta característica, asigna el nombre de tu constante a un parámetro clave, y define el tipo como constant.

  • XML
    <?xml version="1.0" encoding="UTF-8"?>
    
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
        <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>
    
  • PHP
    $container->setParameter('global.constant.value', GLOBAL_CONSTANT);
    $container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME);
    

Nota

This does not works for Yaml configuration. If you’re using Yaml, you can import an XML file to take advantage of this functionality:

  • YAML
    # app/config/config.yml
    imports:
        - { resource: parameters.xml }
    
Bifúrcame en GitHub