|
| 1 | +.. index:: |
| 2 | + single: Dependency Injection |
| 3 | + |
| 4 | +The Dependency Injection Component |
| 5 | +================================== |
| 6 | + |
| 7 | + The Dependency Injection component allows you to standardize and centralize |
| 8 | + the way objects are constructed in your application. |
| 9 | + |
| 10 | +For an introduction to Dependency Injection and service containers see |
| 11 | +:doc:`/book/service_container` |
| 12 | + |
| 13 | +Installation |
| 14 | +------------ |
| 15 | + |
| 16 | +You can install the component in many different ways: |
| 17 | + |
| 18 | +* Use the official Git repository (https://github.com/symfony/DependencyInjection); |
| 19 | +* Install it via PEAR ( `pear.symfony.com/DependencyInjection`); |
| 20 | +* Install it via Composer (`symfony/dependency-injection` on Packagist). |
| 21 | + |
| 22 | +Basic Usage |
| 23 | +----------- |
| 24 | + |
| 25 | +You might have a simple class like the following ``Mailer`` that |
| 26 | +you want to make available as a service: |
| 27 | + |
| 28 | +.. code-block:: php |
| 29 | +
|
| 30 | + class Mailer |
| 31 | + { |
| 32 | + private $transport; |
| 33 | +
|
| 34 | + public function __construct() |
| 35 | + { |
| 36 | + $this->transport = 'sendmail'; |
| 37 | + } |
| 38 | +
|
| 39 | + // ... |
| 40 | + } |
| 41 | +
|
| 42 | +You can register this in the container as a service: |
| 43 | + |
| 44 | +.. code-block:: php |
| 45 | +
|
| 46 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 47 | +
|
| 48 | + $sc = new ContainerBuilder(); |
| 49 | + $sc->register('mailer', 'Mailer'); |
| 50 | +
|
| 51 | +An improvement to the class to make it more flexible would be to allow |
| 52 | +the container to set the ``transport`` used. If you change the class |
| 53 | +so this is passed into the constructor: |
| 54 | + |
| 55 | +.. code-block:: php |
| 56 | +
|
| 57 | + class Mailer |
| 58 | + { |
| 59 | + private $transport; |
| 60 | +
|
| 61 | + public function __construct($transport) |
| 62 | + { |
| 63 | + $this->transport = $transport; |
| 64 | + } |
| 65 | +
|
| 66 | + // ... |
| 67 | + } |
| 68 | +
|
| 69 | +Then you can set the choice of transport in the container: |
| 70 | + |
| 71 | +.. code-block:: php |
| 72 | +
|
| 73 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 74 | +
|
| 75 | + $sc = new ContainerBuilder(); |
| 76 | + $sc->register('mailer', 'Mailer') |
| 77 | + ->addArgument('sendmail')); |
| 78 | +
|
| 79 | +This class is now much more flexible as we have separated the choice of |
| 80 | +transport out of the implementation and into the container. |
| 81 | + |
| 82 | +Which mail transport you have chosen may be something other services need to |
| 83 | +know about. You can avoid having to change it in multiple places by making |
| 84 | +it a parameter in the container and then referring to this parameter for the |
| 85 | +``Mailer`` service's constructor argument: |
| 86 | + |
| 87 | + |
| 88 | +.. code-block:: php |
| 89 | +
|
| 90 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 91 | +
|
| 92 | + $sc = new ContainerBuilder(); |
| 93 | + $sc->setParameter('mailer.transport', 'sendmail'); |
| 94 | + $sc->register('mailer', 'Mailer') |
| 95 | + ->addArgument('%mailer.transport%')); |
| 96 | +
|
| 97 | +Now that the ``mailer`` service is in the container you can inject it as |
| 98 | +a dependency of other classes. If you have a ``NewsletterManager`` class |
| 99 | +like this: |
| 100 | + |
| 101 | +.. code-block:: php |
| 102 | +
|
| 103 | + use Mailer; |
| 104 | +
|
| 105 | + class NewsletterManager |
| 106 | + { |
| 107 | + private $mailer; |
| 108 | +
|
| 109 | + public function __construct(Mailer $mailer) |
| 110 | + { |
| 111 | + $this->mailer = $mailer; |
| 112 | + } |
| 113 | +
|
| 114 | + // ... |
| 115 | + } |
| 116 | +
|
| 117 | +Then you can register this as a service as well and pass the ``mailer`` service into it: |
| 118 | + |
| 119 | +.. code-block:: php |
| 120 | +
|
| 121 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 122 | + use Symfony\Component\DependencyInjection\Reference; |
| 123 | +
|
| 124 | + $sc = new ContainerBuilder(); |
| 125 | +
|
| 126 | + $sc->setParameter('mailer.transport', 'sendmail'); |
| 127 | + $sc->register('mailer', 'Mailer') |
| 128 | + ->addArgument('%mailer.transport%')); |
| 129 | +
|
| 130 | + $sc->register('newsletter_manager', 'NewsletterManager') |
| 131 | + ->addArgument(new Reference('mailer')); |
| 132 | +
|
| 133 | +If the ``NewsletterManager`` did not require the ``Mailer`` and injecting |
| 134 | +it was only optional then you could use setter injection instead: |
| 135 | + |
| 136 | +.. code-block:: php |
| 137 | +
|
| 138 | + use Mailer; |
| 139 | +
|
| 140 | + class NewsletterManager |
| 141 | + { |
| 142 | + private $mailer; |
| 143 | +
|
| 144 | + public function setMailer(Mailer $mailer) |
| 145 | + { |
| 146 | + $this->mailer = $mailer; |
| 147 | + } |
| 148 | +
|
| 149 | + // ... |
| 150 | + } |
| 151 | +
|
| 152 | +You can now choose not to inject a ``Mailer`` into the ``NewsletterManager``. |
| 153 | +If you do want to though then the container can call the setter method: |
| 154 | + |
| 155 | +.. code-block:: php |
| 156 | +
|
| 157 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 158 | + use Symfony\Component\DependencyInjection\Reference; |
| 159 | +
|
| 160 | + $sc = new ContainerBuilder(); |
| 161 | +
|
| 162 | + $sc->setParameter('mailer.transport', 'sendmail'); |
| 163 | + $sc->register('mailer', 'Mailer') |
| 164 | + ->addArgument('%mailer.transport%')); |
| 165 | +
|
| 166 | + $sc->register('newsletter_manager', 'NewsletterManager') |
| 167 | + ->addMethodCall('setMailer', new Reference('mailer')); |
| 168 | +
|
| 169 | +You could then get your ``newsletter_manager`` service from the container |
| 170 | +like this: |
| 171 | + |
| 172 | +.. code-block:: php |
| 173 | +
|
| 174 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 175 | + use Symfony\Component\DependencyInjection\Reference; |
| 176 | +
|
| 177 | + $sc = new ContainerBuilder(); |
| 178 | +
|
| 179 | + //-- |
| 180 | +
|
| 181 | + $newsletterManager = $sc->get('newsletter_manager'); |
| 182 | +
|
| 183 | +Avoiding Your Code Becoming Dependent on the Container |
| 184 | +------------------------------------------------------ |
| 185 | + |
| 186 | +Whilst you can retrieve services from the container directly it is best |
| 187 | +to minimize this. For example, in the ``NewsletterManager`` we injected |
| 188 | +the ``mailer`` service in rather than asking for it from the container. |
| 189 | +We could have injected the container in and retrieved the ``mailer`` service |
| 190 | +from it but it would then be tied to this particular container making it |
| 191 | +difficult to reuse the class elsewhere. |
| 192 | + |
| 193 | +You will need to get a service from the container at some point but this |
| 194 | +should be as few times as possible at the entry point to your application. |
| 195 | + |
| 196 | +Setting Up the Container with Configuration Files |
| 197 | +------------------------------------------------- |
| 198 | + |
| 199 | +As well as setting up the services using PHP as above you can also use configuration |
| 200 | +files. To do this you also need to install the Config component: |
| 201 | + |
| 202 | +* Use the official Git repository (https://github.com/symfony/Config); |
| 203 | +* Install it via PEAR ( `pear.symfony.com/Config`); |
| 204 | +* Install it via Composer (`symfony/config` on Packagist). |
| 205 | + |
| 206 | +Loading an xml config file: |
| 207 | + |
| 208 | +.. code-block:: php |
| 209 | +
|
| 210 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 211 | + use Symfony\Component\Config\FileLocator; |
| 212 | + use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 213 | +
|
| 214 | + $sc = new ContainerBuilder(); |
| 215 | + $loader = new XmlFileLoader($container, new FileLocator(__DIR__)); |
| 216 | + $loader->load('services.xml'); |
| 217 | +
|
| 218 | +Loading a yaml config file: |
| 219 | + |
| 220 | +.. code-block:: php |
| 221 | +
|
| 222 | + use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 223 | + use Symfony\Component\Config\FileLocator; |
| 224 | + use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
| 225 | +
|
| 226 | + $sc = new ContainerBuilder(); |
| 227 | + $loader = new YamlFileLoader($container, new FileLocator(__DIR__)); |
| 228 | + $loader->load('services.yml'); |
| 229 | +
|
| 230 | +The ``newsletter_manager`` and `` mailer`` services can be set up using config files: |
| 231 | + |
| 232 | +.. configuration-block:: |
| 233 | + |
| 234 | + .. code-block:: yaml |
| 235 | +
|
| 236 | + # src/Acme/HelloBundle/Resources/config/services.yml |
| 237 | + parameters: |
| 238 | + # ... |
| 239 | + mailer.transport: sendmail |
| 240 | +
|
| 241 | + services: |
| 242 | + my_mailer: |
| 243 | + class: Mailer |
| 244 | + arguments: [@mailer] |
| 245 | + newsletter_manager: |
| 246 | + class: NewsletterManager |
| 247 | + calls: |
| 248 | + - [ setMailer, [ @mailer ] ] |
| 249 | +
|
| 250 | + .. code-block:: xml |
| 251 | +
|
| 252 | + <!-- src/Acme/HelloBundle/Resources/config/services.xml --> |
| 253 | + <parameters> |
| 254 | + <!-- ... --> |
| 255 | + <parameter key="mailer.transport">sendmail</parameter> |
| 256 | + </parameters> |
| 257 | +
|
| 258 | + <services> |
| 259 | + <service id="mailer" class="Mailer"> |
| 260 | + <argument>%mailer.transport%</argument> |
| 261 | + </service> |
| 262 | +
|
| 263 | + <service id="newsletter_manager" class="NewsletterManager"> |
| 264 | + <call method="setMailer"> |
| 265 | + <argument type="service" id="mailer" /> |
| 266 | + </call> |
| 267 | + </service> |
| 268 | + </services> |
| 269 | +
|
| 270 | + .. code-block:: php |
| 271 | +
|
| 272 | + use Symfony\Component\DependencyInjection\Reference; |
| 273 | +
|
| 274 | + // ... |
| 275 | + $sc->setParameter('mailer.transport', 'sendmail'); |
| 276 | + $sc->register('mailer', 'Mailer') |
| 277 | + ->addArgument('%mailer.transport%')); |
| 278 | +
|
| 279 | + $sc->register('newsletter_manager', 'NewsletterManager') |
| 280 | + ->addMethodCall('setMailer', new Reference('mailer')); |
| 281 | +
|
| 282 | +
|
| 283 | +Learn more from the Cookbook |
| 284 | +---------------------------- |
| 285 | + |
| 286 | +* :doc:`/cookbook/service_container/factories` |
| 287 | +* :doc:`/cookbook/service_container/parentservices` |
0 commit comments