@@ -25,7 +25,7 @@ Fetching and using Services
25
25
26
26
The moment you start a Symfony app, your container *already * contains many services.
27
27
These are like *tools *, waiting for you to take advantage of them. In your controller,
28
- you can "ask" for a service from the container by type-hinting an argument wit the
28
+ you can "ask" for a service from the container by type-hinting an argument with the
29
29
service's class or interface name. Want to :doc: `log </logging >` something? No problem::
30
30
31
31
// src/AppBundle/Controller/ProductController.php
@@ -155,7 +155,7 @@ the service container *how* to instantiate it:
155
155
156
156
# loads services from whatever directories you want (you can update this!)
157
157
AppBundle\ :
158
- resource : ' ../../src/AppBundle/{Service,EventDispatcher, Twig,Form }'
158
+ resource : ' ../../src/AppBundle/{Service,Command,Form,EventSubscriber, Twig,Security }'
159
159
160
160
.. code-block :: xml
161
161
@@ -171,14 +171,20 @@ the service container *how* to instantiate it:
171
171
<defaults autowire =" true" autoconfigure =" true" public =" false" />
172
172
173
173
<!-- Load services from whatever directories you want (you can update this!) -->
174
- <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,EventDispatcher, Twig,Form }" />
174
+ <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,Command,Form,EventSubscriber, Twig,Security }" />
175
175
</services >
176
176
</container >
177
177
178
178
.. code-block :: php
179
179
180
180
// app/config/services.php
181
181
// _defaults and loading entire directories is not possible with PHP configuration
182
+ // you need to define your servicess one-by-one
183
+ use AppBundle/Service/MessageGenerator;
184
+
185
+ $container->autowire(MessageGenerator::class)
186
+ ->setAutoconfigured(true)
187
+ ->setPublic(false);
182
188
183
189
.. versionadded :: 3.3
184
190
The ``_defaults `` key and ability to load services from a directory were added
@@ -228,18 +234,20 @@ be its class name in this case::
228
234
}
229
235
}
230
236
231
- However, this only works if you set your service to be :ref: `public <container-public >`.
237
+ However, this only works if you make your service :ref: `public <container-public >`.
232
238
233
239
.. caution ::
234
240
235
241
Service ids are case-insensitive (e.g. ``AppBundle\Service\MessageGenerator ``
236
242
and ``appbundle\service\messagegenerator `` refer to the same service). But this
237
243
was deprecated in Symfony 3.3. Starting in 4.0, service ids will be case sensitive.
238
244
245
+ .. _services-constructor-injection :
246
+
239
247
Injecting Services/Config into a Service
240
248
----------------------------------------
241
249
242
- What if need to access the ``logger `` service from within ``MessageGenerator ``?
250
+ What if you need to access the ``logger `` service from within ``MessageGenerator ``?
243
251
Your service does *not * have access to the container directly, so you can't fetch
244
252
it via ``$this->container->get() ``.
245
253
@@ -272,17 +280,17 @@ when instantiating the ``MessageGenerator``. How does it know to do this?
272
280
:doc: `Autowiring </service_container/autowiring >`. The key is the ``LoggerInterface ``
273
281
type-hint in your ``__construct() `` method and the ``autowire: true `` config in
274
282
``services.yml ``. When you type-hint an argument, the container will automatically
275
- find the matching service. If it can't or there is any ambiguity , you'll see a clear
276
- exception with a helpful suggestion.
283
+ find the matching service. If it can't, you'll see a clear exception with a helpful
284
+ suggestion.
277
285
278
286
Be sure to read more about :doc: `autowiring </service_container/autowiring >`.
279
287
280
288
.. tip ::
281
289
282
290
How should you know to use ``LoggerInterface `` for the type-hint? The best way
283
291
is by reading the docs for whatever feature you're using. You can also use the
284
- ``php bin/console debug:container `` console command to get a hint
285
- to the class name for a service .
292
+ ``php bin/console debug:container --types `` console command to get a list of
293
+ available type-hints .
286
294
287
295
Handling Multiple Services
288
296
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -337,7 +345,7 @@ the new ``Updates`` sub-directory:
337
345
338
346
# registers all classes in Services & Updates directories
339
347
AppBundle\ :
340
- resource : ' ../../src/AppBundle/{Service,Updates,EventDispatcher, Twig,Form }'
348
+ resource : ' ../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber, Twig,Security }'
341
349
342
350
.. code-block :: xml
343
351
@@ -352,7 +360,7 @@ the new ``Updates`` sub-directory:
352
360
<!-- ... -->
353
361
354
362
<!-- Registers all classes in Services & Updates directories -->
355
- <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,Updates,EventDispatcher, Twig,Form }" />
363
+ <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber, Twig,Security }" />
356
364
</services >
357
365
</container >
358
366
@@ -370,7 +378,7 @@ Now, you can use the service immediately::
370
378
}
371
379
372
380
Thanks to autowiring and your type-hints in ``__construct() ``, the container creates
373
- the ``SiteUpdateManager `` object and passes it the correct arguments . In most cases,
381
+ the ``SiteUpdateManager `` object and passes it the correct argument . In most cases,
374
382
this works perfectly.
375
383
376
384
Manually Wiring Arguments
@@ -428,7 +436,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428
436
429
437
# same as before
430
438
AppBundle\ :
431
- resource : ' ../../src/AppBundle/{Service,Updates}'
439
+ resource : ' ../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security }'
432
440
433
441
# explicitly configure the service
434
442
AppBundle\Updates\SiteUpdateManager :
@@ -448,7 +456,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
448
456
<!-- ... -->
449
457
450
458
<!-- Same as before -->
451
- <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,Updates}" />
459
+ <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security }" />
452
460
453
461
<!-- Explicitly configure the service -->
454
462
<service id =" AppBundle\Updates\SiteUpdateManager" >
@@ -526,6 +534,8 @@ and reference it with the ``%parameter_name%`` syntax:
526
534
527
535
.. code-block :: php
528
536
537
+ // app/config/services.php
538
+ use AppBundle\Updates\SiteUpdateManager;
529
539
$container->setParameter('admin_email', 'manager@example.com');
530
540
531
541
$container->autowire(SiteUpdateManager::class)
@@ -659,7 +669,7 @@ The autoconfigure Option
659
669
Above, we've set ``autoconfigure: true `` in the ``_defaults `` section so that it
660
670
applies to all services defined in that file. With this setting, the container will
661
671
automatically apply certain configuration to your services, based on your service's
662
- *class *. The is mostly used to *auto-tag * your services.
672
+ *class *. This is mostly used to *auto-tag * your services.
663
673
664
674
For example, to create a Twig Extension, you need to create a class, register it
665
675
as a service, and :doc: `tag </service_container/tags >` it with ``twig.extension ``:
@@ -702,7 +712,8 @@ as a service, and :doc:`tag </service_container/tags>` it with ``twig.extension`
702
712
->addTag('twig.extension');
703
713
704
714
But, with ``autoconfigure: true ``, you don't need the tag. In fact, all you need
705
- to do is load your service from the ``Twig `` directory:
715
+ to do is load your service from the ``Twig `` directory, which is already loaded
716
+ by default in a fresh Symfony install:
706
717
707
718
.. configuration-block ::
708
719
@@ -716,7 +727,7 @@ to do is load your service from the ``Twig`` directory:
716
727
717
728
# load your services from the Twig directory
718
729
AppBundle\ :
719
- resource : ' ../../src/AppBundle/{Service,EventDispatcher,Twig ,Form}'
730
+ resource : ' ../../src/AppBundle/{Service,Updates,Command ,Form,EventSubscriber,Twig,Security }'
720
731
721
732
.. code-block :: xml
722
733
@@ -731,7 +742,7 @@ to do is load your service from the ``Twig`` directory:
731
742
<defaults autowire =" true" autoconfigure =" true" />
732
743
733
744
<!-- Load your services-->
734
- <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,EventDispatcher,Twig ,Form}" />
745
+ <prototype namespace =" AppBundle\" resource =" ../../src/AppBundle/{Service,Updates,Command ,Form,EventSubscriber,Twig,Security }" />
735
746
</services >
736
747
</container >
737
748
0 commit comments