8000 minor #7656 [DependencyInjection] Document FQCN aliases (GuilhemN) · symfony/symfony-docs@f9dafd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f9dafd6

Browse files
committed
minor #7656 [DependencyInjection] Document FQCN aliases (GuilhemN)
This PR was merged into the master branch. Discussion ---------- [DependencyInjection] Document FQCN aliases Fix #7445 > Not only do we need to remove autowiring-types, we should show clearly how you can use aliases to choose what class should be autowired for a specific interface/class. > > Also, Stof noted that you can/should mark these aliases as private: we don't need them to be available on the final, cached container. Commits ------- a530019 [DependencyInjection] Document FQCN aliases
2 parents 5788863 + a530019 commit f9dafd6

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

service_container/autowiring.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ and a Twitter client using it:
307307
308308
services:
309309
rot13_transformer:
310-
class: Acme\Rot13Transformer
311-
autowiring_types: Acme\TransformerInterface
310+
class: Acme\Rot13Transformer
311+
312+
Acme\TransformerInterface: '@rot13_transformer'
312313
313314
twitter_client:
314315
class: Acme\TwitterClient
@@ -330,9 +331,9 @@ and a Twitter client using it:
330331
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
331332
332333
<services>
333-
<service id="rot13_transformer" class="Acme\Rot13Transformer">
334-
<autowiring-type>Acme\TransformerInterface</autowiring-type>
335-
</service>
334+
<service id="rot13_transformer" class="Acme\Rot13Transformer" />
335+
336+
<service id="Acme\TransformerInterface" alias="rot13_transformer" />
336337
337338
<service id="twitter_client" class="Acme\TwitterClient" autowire="true" />
338339
@@ -356,9 +357,8 @@ and a Twitter client using it:
356357
use Symfony\Component\DependencyInjection\Definition;
357358
358359
// ...
359-
$rot13Definition = new Definition(Rot13Transformer::class);
360-
$rot13Definition->setAutowiringTypes(array(TransformerInterface::class));
361-
$container->setDefinition('rot13_transformer', $rot13Definition);
360+
$container->register('rot13_transformer', Rot13Transformer::class);
361+
$container->setAlias(TransformerInterface::class, 'rot13_transformer')
362362
363363
$clientDefinition = new Definition(TwitterClient::class);
364364
$clientDefinition->setAutowired(true);
@@ -382,10 +382,14 @@ to use which leads to errors like this:
382382
[Symfony\Component\DependencyInjection\Exception\RuntimeException]
383383
Unable to autowire argument of type "Acme\TransformerInterface" for the service "twitter_client".
384384
385-
Fortunately, the ``autowiring_types`` key is here to specify which implementation
386-
to use by default. This key can take a list of types if necessary.
385+
Fortunately, the FQCN alias is here to specify which implementation
386+
to use by default.
387+
388+
.. versionadded:: 3.3
389+
Using FQCN aliases to fix autowiring ambiguities is allowed since Symfony
390+
3.3. Prior to version 3.3, you needed to use the ``autowiring_types`` key.
387391

388-
Thanks to this setting, the ``rot13_transformer`` service is automatically injected
392+
Thanks to this alias, the ``rot13_transformer`` service is automatically injected
389393
as an argument of the ``uppercase_transformer`` and ``twitter_client`` services. For
390394
the ``uppercase_twitter_client``, a standard service definition is used to
391395
inject the specific ``uppercase_transformer`` service.

0 commit comments

Comments
 (0)
0