|
4 | 4 | Defining Services Dependencies Automatically |
5 | 5 | ============================================ |
6 | 6 |
|
| 7 | +.. versionadded:: 2.8 |
| 8 | + Support for autowiring services was introduced in Symfony 2.8. |
| 9 | + |
7 | 10 | Autowiring allows to register services in the container with minimal configuration. |
8 | | -It is useful in the field of `Rapid Application Development`_, when designing prototypes |
9 | | -in early stages of large projects. It makes it easy to register a service graph |
10 | | -and eases refactoring. |
| 11 | +It automatically resolves the service dependencies based on the constructor's |
| 12 | +typehint which is useful in the field of `Rapid Application Development`_, |
| 13 | +when designing prototypes in early stages of large projects. It makes it easy |
| 14 | +to register a service graph and eases refactoring. |
11 | 15 |
|
12 | | -Imagine you're building an API to publish statuses on a Twitter feed, obfuscated |
13 | | -with `ROT13`.. (a special case of the Caesar cipher). |
| 16 | +Imagine you're building an API to publish statuses on a Twitter feed, which |
| 17 | +has to be obfuscated with ``ROT13`` (a special case of the Caesar cipher). |
14 | 18 |
|
15 | 19 | Start by creating a ROT13 transformer class:: |
16 | 20 |
|
@@ -47,8 +51,9 @@ And now a Twitter client using this transformer:: |
47 | 51 | } |
48 | 52 | } |
49 | 53 |
|
50 | | -The Dependency Injection Component will be able to automatically register the dependencies |
51 | | -of this ``TwitterClient`` class by marking the ``twitter_client`` service as autowired: |
| 54 | +The DependencyInjection component will be able to automatically register |
| 55 | +the dependencies of this ``TwitterClient`` class when the ``twitter_client`` |
| 56 | +service is marked as autowired: |
52 | 57 |
|
53 | 58 | .. configuration-block:: |
54 | 59 |
|
@@ -133,9 +138,11 @@ Here is a typical controller using the ``twitter_client`` service:: |
133 | 138 | } |
134 | 139 | } |
135 | 140 |
|
136 | | -You can give a try to the API with ``curl``:: |
| 141 | +You can give the API a try using ``curl``: |
| 142 | + |
| 143 | +.. code-block:: bash |
137 | 144 |
|
138 | | - curl -d "user=kevin&key=ABCD&status=Hello" http://localhost:8000/tweet |
| 145 | + $ curl -d "user=kevin&key=ABCD&status=Hello" http://localhost:8000/tweet |
139 | 146 |
|
140 | 147 | It should return ``OK``. |
141 | 148 |
|
@@ -316,20 +323,20 @@ and a Twitter client using it:: |
316 | 323 | # app/config/services.yml |
317 | 324 | services: |
318 | 325 | rot13_transformer: |
319 | | - class: 'AppBundle\Rot13Transformer' |
320 | | - autowiring_types: 'AppBundle\TransformerInterface' |
| 326 | + class: AppBundle\Rot13Transformer<
10BC0
/div> |
| 327 | + autowiring_types: AppBundle\TransformerInterface |
321 | 328 |
|
322 | 329 | twitter_client: |
323 | | - class: 'AppBundle\TwitterClient' |
| 330 | + class: AppBundle\TwitterClient |
324 | 331 | autowire: true |
325 | 332 |
|
326 | 333 | uppercase_rot13_transformer: |
327 | | - class: 'AppBundle\UppercaseRot13Transformer' |
| 334 | + class: AppBundle\UppercaseRot13Transformer |
328 | 335 | autowire: true |
329 | 336 |
|
330 | 337 | uppercase_twitter_client: |
331 | | - class: 'AppBundle\TwitterClient' |
332 | | - arguments: [ '@uppercase_rot13_transformer' ] |
| 338 | + class: AppBundle\TwitterClient |
| 339 | + arguments: ['@uppercase_rot13_transformer'] |
333 | 340 |
|
334 | 341 | .. code-block:: xml |
335 | 342 |
|
@@ -373,16 +380,17 @@ and a Twitter client using it:: |
373 | 380 | $definition4->addArgument(new Reference('uppercase_rot13_transformer')); |
374 | 381 | $container->setDefinition('uppercase_twitter_client', $definition4); |
375 | 382 |
|
376 | | -It deserves some explanations. We now have 2 services implementing the ``TransformerInterface``. |
377 | | -The autowiring subsystem cannot guess which one to use, this leads to errors |
378 | | -like:: |
| 383 | +This deserves some explanations. You now have two services implementing the |
| 384 | +``TransformerInterface``. The autowiring subsystem cannot guess which one |
| 385 | +to use which leads to errors like this: |
| 386 | + |
| 387 | +.. code-block:: text |
379 | 388 |
|
380 | 389 | [Symfony\Component\DependencyInjection\Exception\RuntimeException] |
381 | 390 | Unable to autowire argument of type "AppBundle\TransformerInterface" for the service "twitter_client". |
382 | 391 |
|
383 | 392 | Fortunately, the ``autowiring_types`` key is here to specify which implementation |
384 | | -to use by default. This key can take a list of types if necessary (using a YAML |
385 | | -array). |
| 393 | +to use by default. This key can take a list of types if necessary. |
386 | 394 |
|
387 | 395 | Thanks to this setting, the ``rot13_transformer`` service is automatically injected |
388 | 396 | as an argument of the ``uppercase_rot13_transformer`` and ``twitter_client`` services. For |
|
0 commit comments