|
| 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 | + </services> |
| 89 | +
|
| 90 | + .. code-block:: php |
| 91 | +
|
| 92 | + use Symfony\Component\DependencyInjection\Reference; |
| 93 | +
|
| 94 | + // ... |
| 95 | + $container->setParameter('mailer.transport', 'sendmail'); |
| 96 | + $container |
| 97 | + ->register('mailer', 'Mailer') |
| 98 | + ->addArgument('%mailer.transport%'); |
| 99 | +
|
| 100 | +If we were using this elsewhere as well, then it would only need changing |
| 101 | +in one place if a different transport was required. |
| 102 | + |
| 103 | +You can also use the parameters in the service definition, for example, |
| 104 | +making the class of a service a parameter: |
| 105 | + |
| 106 | +.. configuration-block:: |
| 107 | + |
| 108 | + .. code-block:: yaml |
| 109 | +
|
| 110 | + parameters: |
| 111 | + mailer.transport: sendmail |
| 112 | + mailer.class: Mailer |
| 113 | +
|
| 114 | + services: |
| 115 | + mailer: |
| 116 | + class: '%mailer.class%' |
| 117 | + arguments: ['%mailer.transport%'] |
| 118 | +
|
| 119 | + .. code-block:: xml |
| 120 | +
|
| 121 | + <parameters> |
| 122 | + <parameter key="mailer.transport">sendmail</parameter> |
| 123 | + <parameter key="mailer.class">Mailer</parameter> |
| 124 | + </parameters> |
| 125 | +
|
| 126 | + <services> |
| 127 | + <service id="mailer" class="%mailer.class%"> |
| 128 | + <argument>%mailer.transport%</argument> |
| 129 | + </service> |
| 130 | +
|
| 131 | + </services> |
| 132 | +
|
| 133 | + .. code-block:: php |
| 134 | +
|
| 135 | + use Symfony\Component\DependencyInjection\Reference; |
| 136 | +
|
| 137 | + // ... |
| 138 | + $container->setParameter('mailer.transport', 'sendmail'); |
| 139 | + $container->setParameter('mailer.class', 'Mailer'); |
| 140 | + $container |
| 141 | + ->register('mailer', '%mailer.class%') |
| 142 | + ->addArgument('%mailer.transport%'); |
| 143 | +
|
| 144 | + $container |
| 145 | + ->register('newsletter_manager', 'NewsletterManager') |
| 146 | + ->addMethodCall('setMailer', array(new Reference('mailer'))); |
| 147 | +
|
| 148 | +.. note:: |
| 149 | + |
| 150 | + The percent sign inside a parameter or argument, as part of the string, must |
| 151 | + be escaped with another percent sign: |
| 152 | + |
| 153 | + .. configuration-block:: |
| 154 | + |
| 155 | + .. code-block:: yaml |
| 156 | +
|
| 157 | + arguments: ['http://symfony.com/?foo=%%s&bar=%%d'] |
| 158 | +
|
| 159 | + .. code-block:: xml |
| 160 | +
|
| 161 | + <argument type="string">http://symfony.com/?foo=%%s&bar=%%d</argument> |
| 162 | +
|
| 163 | + .. code-block:: php |
| 164 | +
|
| 165 | + ->addArgument('http://symfony.com/?foo=%%s&bar=%%d'); |
| 166 | +
|
| 167 | +Array Parameters |
| 168 | +---------------- |
| 169 | + |
| 170 | +Parameters do not need to be flat strings, they can also be arrays. For the XML |
| 171 | +format, you need to use the ``type="collection"`` attribute for all parameters that are |
| 172 | +arrays. |
| 173 | + |
| 174 | +.. configuration-block:: |
| 175 | + |
| 176 | + .. code-block:: yaml |
| 177 | +
|
| 178 | + # app/config/config.yml |
| 179 | + parameters: |
| 180 | + my_mailer.gateways: |
| 181 | + - mail1 |
| 182 | + - mail2 |
| 183 | + - mail3 |
| 184 | + my_multilang.language_fallback: |
| 185 | + en: |
| 186 | + - en |
| 187 | + - fr |
| 188 | + fr: |
| 189 | + - fr |
| 190 | + - en |
| 191 | +
|
| 192 | + .. code-block:: xml |
| 193 | +
|
| 194 | + <!-- app/config/config.xml --> |
| 195 | + <parameters> |
| 196 | + <parameter key="my_mailer.gateways" type="collection"> |
| 197 | + <parameter>mail1</parameter> |
| 198 | + <parameter>mail2</parameter> |
| 199 | + <parameter>mail3</parameter> |
| 200 | + </parameter> |
| 201 | + <parameter key="my_multilang.language_fallback" type="collection"> |
| 202 | + <parameter key="en" type="collection"> |
| 203 | + <parameter>en</parameter> |
| 204 | + <parameter>fr</parameter> |
| 205 | + </parameter> |
| 206 | + <parameter key="fr" type="collection"> |
| 207 | + <parameter>fr</parameter> |
| 208 | + <parameter>en</parameter> |
| 209 | + </parameter> |
| 210 | + </parameter> |
| 211 | + </parameters> |
| 212 | +
|
| 213 | + .. code-block:: php |
| 214 | +
|
| 215 | + // app/config/config.php |
| 216 | + use Symfony\Component\DependencyInjection\Definition; |
| 217 | +
|
| 218 | + $container->setParameter('my_mailer.gateways', array('mail1', 'mail2', 'mail3')); |
| 219 | + $container->setParameter('my_multilang.language_fallback', array( |
| 220 | + 'en' => array('en', 'fr'), |
| 221 | + 'fr' => array('fr', 'en'), |
| 222 | + )); |
| 223 | +
|
| 224 | +Constants as Parameters |
| 225 | +----------------------- |
| 226 | + |
| 227 | +The container also has support for setting PHP constants as parameters. To |
| 228 | +take advantage of this feature, map the name of your constant to a parameter |
| 229 | +key, and define the type as ``constant``. |
| 230 | + |
| 231 | +.. configuration-block:: |
| 232 | + |
| 233 | + .. code-block:: xml |
| 234 | +
|
| 235 | + <?xml version="1.0" encoding="UTF-8"?> |
| 236 | +
|
| 237 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 238 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 239 | +
|
| 240 | + <parameters> |
| 241 | + <parameter key="global.constant.value" type="constant">GLOBAL_CONSTANT</parameter> |
| 242 | + <parameter key="my_class.constant.value" type="constant">My_Class::CONSTANT_NAME</parameter> |
| 243 | + </parameters> |
| 244 | + </container> |
| 245 | +
|
| 246 | + .. code-block:: php |
| 247 | +
|
| 248 | + $container->setParameter('global.constant.value', GLOBAL_CONSTANT); |
| 249 | + $container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME); |
| 250 | +
|
| 251 | +.. note:: |
| 252 | + |
| 253 | + This does not works for Yaml configuration. If you're using Yaml, you can |
| 254 | + import an XML file to take advantage of this functionality: |
| 255 | + |
| 256 | + .. configuration-block:: |
| 257 | + |
| 258 | + .. code-block:: yaml |
| 259 | +
|
| 260 | + # app/config/config.yml |
| 261 | + imports: |
| 262 | + - { resource: parameters.xml } |
0 commit comments