@@ -18,8 +18,7 @@ with `ROT13`_ (a special case of the Caesar cipher).
18
18
19
19
Start by creating a ROT13 transformer class::
20
20
21
- // src/AppBundle/Rot13Transformer.php
22
- namespace AppBundle;
21
+ namespace Acme;
23
22
24
23
class Rot13Transformer
25
24
{
@@ -31,8 +30,7 @@ Start by creating a ROT13 transformer class::
31
30
32
31
And now a Twitter client using this transformer::
33
32
34
- // src/AppBundle/TwitterClient.php
35
- namespace AppBundle;
33
+ namespace Acme;
36
34
37
35
class TwitterClient
38
36
{
@@ -59,22 +57,20 @@ service is marked as autowired:
59
57
60
58
.. code-block :: yaml
61
59
62
- # app/config/services.yml
63
60
services :
64
61
twitter_client :
65
- class : AppBundle \TwitterClient
62
+ class : Acme \TwitterClient
66
63
autowire : true
67
64
68
65
.. code-block :: xml
69
66
70
- <!-- app/config/services.xml -->
71
67
<?xml version =" 1.0" encoding =" UTF-8" ?>
72
68
<container xmlns =" http://symfony.com/schema/dic/services"
73
69
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
74
70
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
75
71
76
72
<services >
77
- <service id =" twitter_client" class =" AppBundle \TwitterClient" autowire =" true" />
73
+ <service id =" twitter_client" class =" Acme \TwitterClient" autowire =" true" />
78
74
</services >
79
75
</container >
80
76
@@ -83,7 +79,7 @@ service is marked as autowired:
83
79
use Symfony\Component\DependencyInjection\Definition;
84
80
85
81
// ...
86
- $definition = new Definition('AppBundle \TwitterClient');
82
+ $definition = new Definition('Acme \TwitterClient');
87
83
$definition->setAutowired(true);
88
84
89
85
$container->setDefinition('twitter_client', $definition);
@@ -106,8 +102,7 @@ and edit related service definitions.
106
102
107
103
Here is a typical controller using the ``twitter_client `` service::
108
104
109
- // src/AppBundle/Controller/DefaultController.php
110
- namespace AppBundle\Controller;
105
+ namespace Acme\Controller;
111
106
112
107
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
113
108
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
@@ -159,8 +154,7 @@ if necessary. It also allows to use other transformers.
159
154
160
155
Let's introduce a ``TransformerInterface ``::
161
156
162
- // src/AppBundle/TransformerInterface.php
163
- namespace AppBundle;
157
+ namespace Acme;
164
158
165
159
interface TransformerInterface
166
160
{
@@ -197,26 +191,24 @@ subsystem isn't able to find itself the interface implementation to register:
197
191
198
192
.. code-block :: yaml
199
193
200
- # app/config/services.yml
201
194
services :
202
195
rot13_transformer :
203
- class : AppBundle \Rot13Transformer
196
+ class : Acme \Rot13Transformer
204
197
205
198
twitter_client :
206
- class : AppBundle \TwitterClient
199
+ class : Acme \TwitterClient
207
200
autowire : true
208
201
209
202
.. code-block :: xml
210
203
211
- <!-- app/config/services.xml -->
212
204
<?xml version =" 1.0" encoding =" UTF-8" ?>
213
205
<container xmlns =" http://symfony.com/schema/dic/services"
214
206
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
215
207
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
216
208
217
209
<services >
218
- <service id =" rot13_transformer" class =" AppBundle \Rot13Transformer" />
219
- <service id =" twitter_client" class =" AppBundle \TwitterClient" autowire =" true" />
210
+ <service id =" rot13_transformer" class =" Acme \Rot13Transformer" />
211
+ <service id =" twitter_client" class =" Acme \TwitterClient" autowire =" true" />
220
212
</services >
221
213
</container >
222
214
@@ -225,10 +217,10 @@ subsystem isn't able to find itself the interface implementation to register:
225
217
use Symfony\Component\DependencyInjection\Definition;
226
218
227
219
// ...
228
- $definition1 = new Definition('AppBundle \Rot13Transformer');
220
+ $definition1 = new Definition('Acme \Rot13Transformer');
229
221
$container->setDefinition('rot13_transformer', $definition1);
230
222
231
- $definition2 = new Definition('AppBundle \TwitterClient');
223
+ $definition2 = new Definition('Acme \TwitterClient');
232
224
$definition2->setAutowired(true);
233
225
$container->setDefinition('twitter_client', $definition2);
234
226
@@ -244,8 +236,7 @@ Last but not least, the autowiring feature allows to specify the default impleme
244
236
of a given type. Let's introduce a new implementation of the ``TransformerInterface ``
245
237
returning the result of the ROT13 transformation uppercased::
246
238
247
- // src/AppBundle/UppercaseRot13Transformer.php
248
- namespace AppBundle;
239
+ namespace Acme;
249
240
250
241
class UppercaseTransformer implements TransformerInterface
251
242
{
@@ -267,8 +258,7 @@ This class is intended to decorate the any transformer and return its value uppe
267
258
We can now refactor the controller to add another endpoint leveraging this new
268
259
transformer::
269
260
270
- // src/AppBundle/Controller/DefaultController.php
271
- namespace AppBundle\Controller;
261
+ namespace Acme\Controller;
272
262
273
263
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
274
264
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
@@ -320,39 +310,37 @@ and a Twitter client using it:
320
310
321
311
.. code-block :: yaml
322
312
323
- # app/config/services.yml
324
313
services :
325
314
rot13_transformer :
326
- class : AppBundle \Rot13Transformer
327
- autowiring_types : AppBundle \TransformerInterface
315
+ class : Acme \Rot13Transformer
316
+ autowiring_types : Acme \TransformerInterface
328
317
329
318
twitter_client :
330
- class : AppBundle \TwitterClient
319
+ class : Acme \TwitterClient
331
320
autowire : true
332
321
333
322
uppercase_rot13_transformer :
334
- class : AppBundle \UppercaseRot13Transformer
323
+ class : Acme \UppercaseRot13Transformer
335
324
autowire : true
336
325
337
326
uppercase_twitter_client :
338
- class : AppBundle \TwitterClient
327
+ class : Acme \TwitterClient
339
328
arguments : ['@uppercase_rot13_transformer']
340
329
341
330
.. code-block :: xml
342
331
343
- <!-- app/config/services.xml -->
344
332
<?xml version =" 1.0" encoding =" UTF-8" ?>
345
333
<container xmlns =" http://symfony.com/schema/dic/services"
346
334
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
347
335
xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
348
336
349
337
<services >
350
- <service id =" rot13_transformer" class =" AppBundle \Rot13Transformer" >
351
- <autowiring-type >AppBundle \TransformerInterface</autowiring-type >
338
+ <service id =" rot13_transformer" class =" Acme \Rot13Transformer" >
339
+ <autowiring-type >Acme \TransformerInterface</autowiring-type >
352
340
</service >
353
- <service id =" twitter_client" class =" AppBundle \TwitterClient" autowire =" true" />
354
- <service id =" uppercase_rot13_transformer" class =" AppBundle \UppercaseRot13Transformer" autowire =" true" />
355
- <service id =" uppercase_twitter_client" class =" AppBundle \TwitterClient" >
341
+ <service id =" twitter_client" class =" Acme \TwitterClient" autowire =" true" />
342
+ <service id =" uppercase_rot13_transformer" class =" Acme \UppercaseRot13Transformer" autowire =" true" />
343
+ <service id =" uppercase_twitter_client" class =" Acme \TwitterClient" >
356
344
<argument type =" service" id =" uppercase_rot13_transformer" />
357
345
</service >
358
346
</services >
@@ -364,19 +352,19 @@ and a Twitter client using it:
364
352
use Symfony\Component\DependencyInjection\Definition;
365
353
366
354
// ...
367
- $definition1 = new Definition('AppBundle \Rot13Transformer');
368
- $definition1->setAutowiringTypes(array('AppBundle \TransformerInterface'));
355
+ $definition1 = new Definition('Acme \Rot13Transformer');
356
+ $definition1->setAutowiringTypes(array('Acme \TransformerInterface'));
369
357
$container->setDefinition('rot13_transformer', $definition1);
370
358
371
- $definition2 = new Definition('AppBundle \TwitterClient');
359
+ $definition2 = new Definition('Acme \TwitterClient');
372
360
$definition2->setAutowired(true);
373
361
$container->setDefinition('twitter_client', $definition2);
374
362
375
- $definition3 = new Definition('AppBundle \UppercaseRot13Transformer');
363
+ $definition3 = new Definition('Acme \UppercaseRot13Transformer');
376
364
$definition3->setAutowired(true);
377
365
$container->setDefinition('uppercase_rot13_transformer', $definition3);
378
366
379
- $definition4 = new Definition('AppBundle \TwitterClient');
367
+ $definition4 = new Definition('Acme \TwitterClient');
380
368
$definition4->addArgument(new Reference('uppercase_rot13_transformer'));
381
369
$container->setDefinition('uppercase_twitter_client', $definition4);
382
370
@@ -387,7 +375,7 @@ to use which leads to errors like this:
387
375
.. code-block :: text
388
376
389
377
[Symfony\Component\DependencyInjection\Exception\RuntimeException]
390
- Unable to autowire argument of type "AppBundle \TransformerInterface" for the service "twitter_client".
378
+ Unable to autowire argument of type "Acme \TransformerInterface" for the service "twitter_client".
391
379
392
380
Fortunately, the ``autowiring_types `` key is here to specify which implementation
393
381
to use by default. This key can take a list of types if necessary.
0 commit comments