Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1e3024

Browse files
committed
[#6032] some tweaks for the autowiring feature
1 parentfb7f15e commitd1e3024

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

‎components/dependency_injection/autowiring.rst

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
Defining Services Dependencies Automatically
55
============================================
66

7+
..versionadded::2.8
8+
Support for autowiring services was introduced in Symfony 2.8.
9+
710
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.
1115

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 obfuscatedwith ``ROT13`` (a special case of the Caesar cipher).
1418

1519
Start by creating a ROT13 transformer class::
1620

@@ -47,8 +51,9 @@ And now a Twitter client using this transformer::
4751
}
4852
}
4953

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:
5257

5358
..configuration-block::
5459

@@ -133,9 +138,11 @@ Here is a typical controller using the ``twitter_client`` service::
133138
}
134139
}
135140

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
137144
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
139146
140147
It should return ``OK``.
141148

@@ -316,20 +323,20 @@ and a Twitter client using it::
316323
# app/config/services.yml
317324
services:
318325
rot13_transformer:
319-
class:'AppBundle\Rot13Transformer'
320-
autowiring_types:'AppBundle\TransformerInterface'
326+
class:AppBundle\Rot13Transformer
327+
autowiring_types:AppBundle\TransformerInterface
321328
322329
twitter_client:
323-
class:'AppBundle\TwitterClient'
330+
class:AppBundle\TwitterClient
324331
autowire:true
325332
326333
uppercase_rot13_transformer:
327-
class:'AppBundle\UppercaseRot13Transformer'
334+
class:AppBundle\UppercaseRot13Transformer
328335
autowire:true
329336
330337
uppercase_twitter_client:
331-
class:'AppBundle\TwitterClient'
332-
arguments:['@uppercase_rot13_transformer']
338+
class:AppBundle\TwitterClient
339+
arguments:['@uppercase_rot13_transformer']
333340
334341
..code-block::xml
335342
@@ -373,16 +380,17 @@ and a Twitter client using it::
373380
$definition4->addArgument(new Reference('uppercase_rot13_transformer'));
374381
$container->setDefinition('uppercase_twitter_client', $definition4);
375382
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
379388
380389
[Symfony\Component\DependencyInjection\Exception\RuntimeException]
381390
Unable to autowire argument of type "AppBundle\TransformerInterface" for the service "twitter_client".
382391
383392
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.
386394

387395
Thanks to this setting, the ``rot13_transformer`` service is automatically injected
388396
as an argument of the ``uppercase_rot13_transformer`` and ``twitter_client`` services. For

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp