|
| 1 | +.. index:: |
| 2 | + single: Dependency Injection; Parameters |
| 3 | + |
| 4 | +Introduction to Parameters |
| 5 | +================================= |
| 6 | + |
| 7 | +You can define parameters in the service container which can then be used |
| 8 | +directly or as part of service definitions. This can help to separate out |
| 9 | +values that you will want to change more regularly. |
| 10 | + |
| 11 | +Getting and Setting Container Parameters |
| 12 | +---------------------------------------- |
| 13 | + |
| 14 | +Working with container parameters is straight forward using the container's |
| 15 | +accessor methods for parameters. You can check if a parameter has been defined |
| 16 | +in the container with:: |
| 17 | + |
| 18 | + $container->hasParameter('mailer.transport'); |
| 19 | + |
| 20 | +You can retrieve parameters set in the container with:: |
| 21 | + |
| 22 | + $container->getParameter('mailer.transport'); |
| 23 | + |
| 24 | +and set a parameter in the container with:: |
| 25 | + |
| 26 | + $container->setParameter('mailer.transport', 'sendmail'); |
| 27 | + |
| 28 | +.. note:: |
| 29 | + |
| 30 | + You can only set a parameter before the container is compiled. To learn |
| 31 | + more about compiling the container see |
| 32 | + :doc:`/components/dependency_injection/compilation` |
| 33 | + |
| 34 | +Parameters in Configuration Files |
| 35 | +--------------------------------- |
| 36 | + |
| 37 | +You can also use the ``parameters`` section of a config file to set parameters: |
| 38 | + |
| 39 | +.. configuration-block:: |
| 40 | + |
| 41 | + .. code-block:: yaml |
| 42 | +
|
| 43 | + parameters: |
| 44 | + mailer.transport: sendmail |
| 45 | +
|
| 46 | + .. code-block:: xml |
| 47 | +
|
| 48 | + <parameters> |
| 49 | + <parameter key="mailer.transport">sendmail</parameter> |
| 50 | + </parameters> |
| 51 | +
|
| 52 | + .. code-block:: php |
| 53 | +
|
| 54 | + $container->setParameter('mailer.transport', 'sendmail'); |
| 55 | +
|
| 56 | +As well as retrieving the parameter values directly from the container you |
| 57 | +can use them in the config files. You can refer to parameters elsewhere in |
| 58 | +the config files by surrounding them with percent (``%``) signs, e.g. |
| 59 | +``%mailer.transport%``. One use is for this is to inject the values into your |
| 60 | +services. This allows you to configure different versions of services between |
| 61 | +applications or multiple services based on the same class but configured |
| 62 | +differently within a single application. You could inject the choice of mail |
| 63 | +transport into the ``Mailer`` class directly but by making it a parameter it |
| 64 | +makes it easier to change rather than being tied up with the service definition: |
| 65 | + |
| 66 | +.. configuration-block:: |
| 67 | + |
| 68 | + .. code-block:: yaml |
| 69 | +
|
| 70 | + parameters: |
| 71 | + mailer.transport: sendmail |
| 72 | +
|
| 73 | + services: |
| 74 | + mailer: |
| 75 | + class: Mailer |
| 76 | + arguments: [%mailer.transport%] |
| 77 | +
|
| 78 | + .. code-block:: xml |
| 79 | +
|
| 80 | + <parameters> |
| 81 | + <parameter key="mailer.transport">sendmail</parameter> |
| 82 | + </parameters> |
| 83 | +
|
| 84 | + <services> |
| 85 | + <service id="mailer" class="Mailer"> |
| 86 | + <argument>%mailer.transport%</argument> |
| 87 | + </service> |
| 88 | +
|
| 89 | + </services> |
| 90 | +
|
| 91 | + .. code-block:: php |
| 92 | +
|
| 93 | + use Symfony\Component\DependencyInjection\Reference; |
| 94 | +
|
| 95 | + // ... |
| 96 | + $container->setParameter('mailer.transport', 'sendmail'); |
| 97 | + $container |
| 98 | + ->register('mailer', 'Mailer') |
| 99 | + ->addArgument('%mailer.transport%'); |
| 100 | +
|
| 101 | +If we were using this elsewhere as well, then it would only need changing |
| 102 | +in one place if a different transport was required. |
| 103 | + |
| 104 | +You can also use the parameters in the service definition, for example, |
| 105 | +making the class of a service a parameter: |
| 106 | + |
| 107 | +.. configuration-block:: |
| 108 | + |
| 109 | + .. code-block:: yaml |
| 110 | +
|
| 111 | + parameters: |
| 112 | + mailer.transport: sendmail |
| 113 | + mailer.class: Mailer |
| 114 | +
|
| 115 | + services: |
| 116 | + mailer: |
| 117 | + class: %mailer.class% |
| 118 | + arguments: [%mailer.transport%] |
| 119 | +
|
| 120 | + .. code-block:: xml |
| 121 | +
|
| 122 | + <parameters> |
| 123 | + <parameter key="mailer.transport">sendmail</parameter> |
| 124 | + <parameter key="mailer.class">Mailer</parameter> |
| 125 | + </parameters> |
| 126 | +
|
| 127 | + <services> |
| 128 | + <service id="mailer" class="%mailer.class%"> |
| 129 | + <argument>%mailer.transport%</argument> |
| 130 | + </service> |
| 131 | +
|
| 132 | + </services> |
| 133 | +
|
| 134 | + .. code-block:: php |
| 135 | +
|
| 136 | + use Symfony\Component\DependencyInjection\Reference; |
| 137 | +
|
| 138 | + // ... |
| 139 | + $container->setParameter('mailer.transport', 'sendmail'); |
| 140 | + $container->setParameter('mailer.class', 'Mailer'); |
| 141 | + $container |
| 142 | + ->register('mailer', '%mailer.class%') |
| 143 | + ->addArgument('%mailer.transport%'); |
| 144 | +
|
| 145 | + $container |
| 146 | + ->register('newsletter_manager', 'NewsletterManager') |
| 147 | + ->addMethodCall('setMailer', array(new Reference('mailer'))); |
| 148 | +
|
| 149 | +.. note:: |
| 150 | + |
| 151 | + The percent sign inside a parameter or argument, as part of the string, must |
| 152 | + be escaped with another percent sign: |
| 153 | + |
| 154 | + .. code-block:: xml |
| 155 | +
|
| 156 | + <argument type="string">http://symfony.com/?foo=%%s&bar=%%d</argument> |
| 157 | +
|
| 158 | +Array Parameters |
| 159 | +---------------- |
| 160 | + |
| 161 | +Parameters do not need to be flat strings, they can also be arrays. For the XML |
| 162 | +format, you need to use the type="collection" attribute for all parameters that are |
| 163 | +arrays. |
| 164 | + |
| 165 | +.. configuration-block:: |
| 166 | + |
| 167 | + .. code-block:: yaml |
| 168 | +
|
| 169 | + # app/config/config.yml |
| 170 | + parameters: |
| 171 | + my_mailer.gateways: |
| 172 | + - mail1 |
| 173 | + - mail2 |
| 174 | + - mail3 |
| 175 | + my_multilang.language_fallback: |
| 176 | + en: |
| 177 | + - en |
| 178 | + - fr |
| 179 | + fr: |
| 180 | + - fr |
| 181 | + - en |
| 182 | +
|
| 183 | + .. code-block:: xml |
| 184 | +
|
| 185 | + <!-- app/config/config.xml --> |
| 186 | + <parameters> |
| 187 | + <parameter key="my_mailer.gateways" type="collection"> |
| 188 | + <parameter>mail1</parameter> |
| 189 | + <parameter>mail2</parameter> |
| 190 | + <parameter>mail3</parameter> |
| 191 | + </parameter> |
| 192 | + <parameter key="my_multilang.language_fallback" type="collection"> |
| 193 | + <parameter key="en" type="collection"> |
| 194 | + <parameter>en</parameter> |
| 195 | + <parameter>fr</parameter> |
| 196 | + </parameter> |
| 197 | + <parameter key="fr" type="collection"> |
| 198 | + <parameter>fr</parameter> |
| 199 | + <parameter>en</parameter> |
| 200 | + </parameter> |
| 201 | + </parameter> |
| 202 | + </parameters> |
| 203 | +
|
| 204 | + .. code-block:: php |
| 205 | +
|
| 206 | + // app/config/config.php |
| 207 | + use Symfony\Component\DependencyInjection\Definition; |
| 208 | +
|
| 209 | + $container->setParameter('my_mailer.gateways', array('mail1', 'mail2', 'mail3')); |
| 210 | + $container->setParameter('my_multilang.language_fallback', array( |
| 211 | + 'en' => array('en', 'fr'), |
| 212 | + 'fr' => array('fr', 'en'), |
| 213 | + )); |
| 214 | +
|
| 215 | +Constants as Parameters |
| 216 | +----------------------- |
| 217 | + |
| 218 | +The container also has support for setting PHP constants as parameters. To |
| 219 | +take advantage of this feature, map the name of your constant to a parameter |
| 220 | +key, and define the type as ``constant``. |
| 221 | + |
| 222 | +.. code-block:: xml |
| 223 | +
|
| 224 | + <?xml version="1.0" encoding="UTF-8"?> |
| 225 | +
|
| 226 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 227 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 228 | +
|
| 229 | + <parameters> |
| 230 | + <parameter key="global.constant.value" type="constant">GLOBAL_CONSTANT</parameter> |
| 231 | + <parameter key="my_class.constant.value" type="constant">My_Class::CONSTANT_NAME</parameter> |
| 232 | + </parameters> |
| 233 | + </container> |
| 234 | +
|
| 235 | +.. note:: |
| 236 | + |
| 237 | + This only works for XML configuration. If you're *not* using XML, simply |
| 238 | + import an XML file to take advantage of this functionality: |
| 239 | + |
| 240 | + .. configuration-block:: |
| 241 | + |
| 242 | + .. code-block:: yaml |
| 243 | +
|
| 244 | + # app/config/config.yml |
| 245 | + imports: |
| 246 | + - { resource: parameters.xml } |
| 247 | +
|
| 248 | + .. code-block:: php |
| 249 | +
|
| 250 | + // app/config/config.php |
| 251 | + $loader->import('parameters.xml'); |
| 252 | +
|
0 commit comments