@@ -15,7 +15,7 @@ with `ROT13`_ (a special case of the Caesar cipher).
1515
1616Start by creating a ROT13 transformer class::
1717
18- namespace Acme ;
18+ namespace AppBundle ;
1919
2020 class Rot13Transformer
2121 {
@@ -27,7 +27,7 @@ Start by creating a ROT13 transformer class::
2727
2828And now a Twitter client using this transformer::
2929
30- namespace Acme ;
30+ namespace AppBundle ;
3131
3232 class TwitterClient
3333 {
@@ -47,16 +47,15 @@ And now a Twitter client using this transformer::
4747 }
4848
4949The DependencyInjection component will be able to automatically register
50- the dependencies of this ``TwitterClient `` class when the `` twitter_client ``
51- service is marked as autowired:
50+ the dependencies of this ``TwitterClient `` class when its service is marked as
51+ autowired:
5252
5353.. configuration-block ::
5454
5555 .. code-block :: yaml
5656
5757 services :
58- twitter_client :
59- class : Acme\TwitterClient
58+ AppBundle\TwitterClient :
6059 autowire : true
6160
6261 .. code-block :: xml
@@ -67,17 +66,17 @@ service is marked as autowired:
6766 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
6867
6968 <services >
70- <service id =" twitter_client " class = " Acme \TwitterClient" autowire =" true" />
69+ <service id =" AppBundle \TwitterClient" autowire =" true" />
7170 </services >
7271 </container >
7372
7473 .. code-block :: php
7574
76- use Acme \TwitterClient;
75+ use AppBundle \TwitterClient;
7776
7877 // ...
7978
80- $container->autowire('twitter_client', TwitterClient::class);
79+ $container->autowire(TwitterClient::class);
8180
8281
8382 .. versionadded :: 3.3
@@ -101,10 +100,11 @@ to change the dependencies of the ``TwitterClient`` class: just add or remove ty
101100arguments in the constructor and you are done. There is no need anymore to search
102101and edit related service definitions.
103102
104- Here is a typical controller using the ``twitter_client `` service ::
103+ Here is a typical controller using the ``TwitterClient `` ::
105104
106- namespace Acme \Controller;
105+ namespace AppBundle \Controller;
107106
107+ use AppBundle\TwitterClient;
108108 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
109109 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
110110 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@@ -118,7 +118,7 @@ Here is a typical controller using the ``twitter_client`` service::
118118 * @Route("/tweet")
119119 * @Method("POST")
120120 */
121- public function tweetAction(Request $request)
121+ public function tweetAction(Request $request, TwitterClient $twitterClient )
122122 {
123123 $user = $request->request->get('user');
124124 $key = $request->request->get('key');
@@ -128,7 +128,10 @@ Here is a typical controller using the ``twitter_client`` service::
128128 throw new BadRequestHttpException();
129129 }
130130
131- $this->get('twitter_client')->tweet($user, $key, $status);
131+ $twitterClient->tweet($user, $key, $status);
132+
133+ // or using the container
134+ // $this->container->get(TwitterClient::class)->tweet($user, $key, $status);
132135
133136 return new Response('OK');
134137 }
@@ -154,7 +157,7 @@ and not concrete classes. It allows to replace easily the current implementation
154157if necessary. It also allows to use other transformers. You can create a
155158``TransformerInterface `` containing just one method (``transform() ``)::
156159
157- namespace Acme ;
160+ namespace AppBundle ;
158161
159162 interface TransformerInterface
160163 {
@@ -189,11 +192,9 @@ subsystem isn't able to find itself the interface implementation to register:
189192 .. code-block :: yaml
190193
191194 services :
192- rot13_transformer :
193- class : Acme\Rot13Transformer
195+ AppBundle\Rot13Transformer : ~
194196
195- twitter_client :
196- class : Acme\TwitterClient
197+ AppBundle\TwitterClient :
197198 autowire : true
198199
199200 .. code-block :: xml
@@ -204,24 +205,25 @@ subsystem isn't able to find itself the interface implementation to register:
204205 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
205206
206207 <services >
207- <service id =" rot13_transformer " class = " Acme \Rot13Transformer" />
208+ <service id =" AppBundle \Rot13Transformer" />
208209
209- <service id =" twitter_client " class = " Acme \TwitterClient" autowire =" true" />
210+ <service id =" AppBundle \TwitterClient" autowire =" true" />
210211 </services >
211212 </container >
212213
213214 .. code-block :: php
214215
215- use Acme\TwitterClient;
216+ use AppBundle\Rot13Transformer;
217+ use AppBundle\TwitterClient;
216218
217219 // ...
218- $container->register('rot13_transformer', 'Acme\ Rot13Transformer' );
219- $container->autowire('twitter_client', TwitterClient::class);
220+ $container->register(Rot13Transformer::class );
221+ $container->autowire(TwitterClient::class);
220222
221- The autowiring subsystem detects that the ``rot13_transformer `` service implements
222- the ``TransformerInterface `` and injects it automatically. Even when using
223- interfaces (and you should), building the service graph and refactoring the project
224- is easier than with standard definitions.
223+ The autowiring subsystem detects that the ``AppBundle\Rot13Transformer `` service
224+ implements the ``TransformerInterface `` and injects it automatically. Even when
225+ using interfaces (and you should), building the service graph and refactoring
226+ the project is easier than with standard definitions.
225227
226228.. _service-autowiring-alias :
227229
@@ -232,7 +234,7 @@ Last but not least, the autowiring feature allows to specify the default impleme
232234of a given type. Let's introduce a new implementation of the ``TransformerInterface ``
233235returning the result of the ROT13 transformation uppercased::
234236
235- namespace Acme ;
237+ namespace AppBundle ;
236238
237239 class UppercaseTransformer implements TransformerInterface
238240 {
@@ -254,8 +256,9 @@ This class is intended to decorate any transformer and return its value uppercas
254256The controller can now be refactored to add a new endpoint using this uppercase
255257transformer::
256258
257- namespace Acme \Controller;
259+ namespace AppBundle \Controller;
258260
261+ use AppBundle\TwitterClient;
259262 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
260263 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
261264 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@@ -269,9 +272,9 @@ transformer::
269272 * @Route("/tweet")
270273 * @Method("POST")
271274 */
272- public function tweetAction(Request $request)
275+ public function tweetAction(Request $request, TwitterClient $twitterClient )
273276 {
274- return $this->tweet($request, 'twitter_client' );
277+ return $this->tweet($request, $twitterClient );
275278 }
276279
277280 /**
@@ -280,10 +283,13 @@ transformer::
280283 */
281284 public function tweetUppercaseAction(Request $request)
282285 {
283- return $this->tweet($request, 'uppercase_twitter_client');
286+ // not the default implementation
287+ $twitterClient = $this->get('uppercase_twitter_client');
288+
289+ return $this->tweet($request, $twitterClient);
284290 }
285291
286- private function tweet(Request $request, $service )
292+ private function tweet(Request $request, TwitterClient $twitterClient )
287293 {
288294 $user = $request->request->get('user');
289295 $key = $request->request->get('key');
@@ -293,7 +299,7 @@ transformer::
293299 throw new BadRequestHttpException();
294300 }
295301
296- $this->get($service) ->tweet($user, $key, $status);
302+ $twitterClient ->tweet($user, $key, $status);
297303
298304 return new Response('OK');
299305 }
@@ -307,22 +313,19 @@ and a Twitter client using it:
307313 .. code-block :: yaml
308314
309315 services :
310- rot13_transformer :
311- class : Acme\Rot13Transformer
316+ AppBundle\Rot13Transformer : ~
312317
313- Acme \TransformerInterface : ' @rot13_transformer '
318+ AppBundle \TransformerInterface : ' @AppBundle\Rot13Transformer '
314319
315- twitter_client :
316- class : Acme\TwitterClient
320+ AppBundle\TwitterClient :
317321 autowire : true
318322
319- uppercase_transformer :
320- class : Acme\UppercaseTransformer
323+ AppBundle\UppercaseTransformer :
321324 autowire : true
322325
323326 uppercase_twitter_client :
324- class : Acme \TwitterClient
325- arguments : ['@uppercase_transformer ']
327+ class : AppBundle \TwitterClient
328+ arguments : ['@AppBundle\UppercaseTransformer ']
326329
327330 .. code-block :: xml
328331
@@ -332,38 +335,36 @@ and a Twitter client using it:
332335 xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
333336
334337 <services >
335- <service id =" rot13_transformer " class = " Acme \Rot13Transformer" />
338+ <service id =" AppBundle \Rot13Transformer" />
336339
337- <service id =" Acme \TransformerInterface" alias =" rot13_transformer " />
340+ <service id =" AppBundle \TransformerInterface" alias =" AppBundle\Rot13Transformer " />
338341
339- <service id =" twitter_client " class = " Acme \TwitterClient" autowire =" true" />
342+ <service id =" AppBundle \TwitterClient" autowire =" true" />
340343
341- <service id =" uppercase_transformer" class =" Acme\UppercaseTransformer"
342- autowire =" true"
343- />
344+ <service id =" AppBundle\UppercaseTransformer" autowire =" true" />
344345
345- <service id =" uppercase_twitter_client" class =" Acme \TwitterClient" >
346- <argument type =" service" id =" uppercase_transformer " />
346+ <service id =" uppercase_twitter_client" class =" AppBundle \TwitterClient" >
347+ <argument type =" service" id =" AppBundle\UppercaseTransformer " />
347348 </service >
348349 </services >
349350 </container >
350351
351352 .. code-block :: php
352353
353- use Acme \Rot13Transformer;
354- use Acme \TransformerInterface;
355- use Acme \TwitterClient;
356- use Acme \UppercaseTransformer;
354+ use AppBundle \Rot13Transformer;
355+ use AppBundle \TransformerInterface;
356+ use AppBundle \TwitterClient;
357+ use AppBundle \UppercaseTransformer;
357358 use Symfony\Component\DependencyInjection\Reference;
358359
359360 // ...
360- $container->register('rot13_transformer', Rot13Transformer::class);
361- $container->setAlias(TransformerInterface::class, 'rot13_transformer' )
361+ $container->register(Rot13Transformer::class);
362+ $container->setAlias(TransformerInterface::class, Rot13Transformer::class )
362363
363- $container->autowire('twitter_client', TwitterClient::class);
364- $container->autowire('uppercase_transformer', UppercaseTransformer::class);
364+ $container->autowire(TwitterClient::class);
365+ $container->autowire(UppercaseTransformer::class);
365366 $container->register('uppercase_twitter_client', TwitterClient::class)
366- ->addArgument(new Reference('uppercase_transformer' ));
367+ ->addArgument(new Reference(UppercaseTransformer::class ));
367368
368369 This deserves some explanations. You now have two services implementing the
369370``TransformerInterface ``. The autowiring subsystem cannot guess which one
@@ -372,19 +373,20 @@ to use which leads to errors like this:
372373.. code-block :: text
373374
374375 [Symfony\Component\DependencyInjection\Exception\RuntimeException]
375- Unable to autowire argument of type "Acme \TransformerInterface" for the service "twitter_client ".
376+ Unable to autowire argument of type "AppBundle \TransformerInterface" for the service "AppBundle\TwitterClient ".
376377
377- Fortunately, the FQCN alias is here to specify which implementation
378- to use by default.
378+ Fortunately, the FQCN alias (the `` AppBundle\TransformerInterface `` alias) is
379+ here to specify which implementation to use by default.
379380
380381.. versionadded :: 3.3
381382 Using FQCN aliases to fix autowiring ambiguities is allowed since Symfony
382383 3.3. Prior to version 3.3, you needed to use the ``autowiring_types `` key.
383384
384- Thanks to this alias, the ``rot13_transformer `` service is automatically injected
385- as an argument of the ``uppercase_transformer `` and ``twitter_client `` services. For
386- the ``uppercase_twitter_client ``, a standard service definition is used to
387- inject the specific ``uppercase_transformer `` service.
385+ Thanks to this alias, the ``AppBundle\Rot13Transformer `` service is
386+ automatically injected as an argument of the ``AppBundle\UppercaseTransformer ``
387+ and ``AppBundle\TwitterClient `` services. For the ``uppercase_twitter_client ``,
388+ a standard service definition is used to inject the specific
389+ ``AppBundle\UppercaseTransformer `` service.
388390
389391As for other RAD features such as the FrameworkBundle controller or annotations,
390392keep in mind to not use autowiring in public bundles nor in large projects with
0 commit comments