@@ -25,7 +25,7 @@ Fetching and using Services
2525
2626The moment you start a Symfony app, your container *already * contains many services.
2727These 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 argumentwit the
28+ you can "ask" for a service from the container by type-hinting an argumentwith the
2929service's class or interface name. Want to:doc: `log </logging >` something? No problem::
3030
3131 // src/AppBundle/Controller/ProductController.php
@@ -155,7 +155,7 @@ the service container *how* to instantiate it:
155155
156156# loads services from whatever directories you want (you can update this!)
157157AppBundle\ :
158- resource :' ../../src/AppBundle/{Service,EventDispatcher, Twig,Form }'
158+ resource :' ../../src/AppBundle/{Service,Command,Form,EventSubscriber, Twig,Security }'
159159
160160 ..code-block ::xml
161161
@@ -171,14 +171,20 @@ the service container *how* to instantiate it:
171171 <defaults autowire =" true" autoconfigure =" true" public =" false" />
172172
173173<!-- 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 }" />
175175 </services >
176176 </container >
177177
178178 ..code-block ::php
179179
180180 // app/config/services.php
181181 // _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);
182188
183189 ..versionadded ::3.3
184190 The ``_defaults `` key and ability to load services from a directory were added
@@ -228,18 +234,20 @@ be its class name in this case::
228234 }
229235 }
230236
231- However, this only works if youset your service to be :ref: `public <container-public >`.
237+ However, this only works if youmake your service:ref: `public <container-public >`.
232238
233239..caution ::
234240
235241 Service ids are case-insensitive (e.g. ``AppBundle\Service\MessageGenerator ``
236242 and ``appbundle\service\messagegenerator `` refer to the same service). But this
237243 was deprecated in Symfony 3.3. Starting in 4.0, service ids will be case sensitive.
238244
245+ .. _services-constructor-injection :
246+
239247Injecting Services/Config into a Service
240248----------------------------------------
241249
242- What if need to access the ``logger `` service from within ``MessageGenerator ``?
250+ What ifyou need to access the ``logger `` service from within ``MessageGenerator ``?
243251Your service does *not * have access to the container directly, so you can't fetch
244252it via ``$this->container->get() ``.
245253
@@ -272,17 +280,17 @@ when instantiating the ``MessageGenerator``. How does it know to do this?
272280:doc: `Autowiring </service_container/autowiring >`. The key is the ``LoggerInterface ``
273281type-hint in your ``__construct() `` method and the ``autowire: true `` config in
274282``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.
277285
278286Be sure to read more about:doc: `autowiring </service_container/autowiring >`.
279287
280288..tip ::
281289
282290 How should you know to use ``LoggerInterface `` for the type-hint? The best way
283291 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 ahint
285- to the class name for a service .
292+ ``php bin/console debug:container --types `` console command to get alist of
293+ available type-hints .
286294
287295Handling Multiple Services
288296~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -337,7 +345,7 @@ the new ``Updates`` sub-directory:
337345
338346# registers all classes in Services & Updates directories
339347AppBundle\ :
340- resource :' ../../src/AppBundle/{Service,Updates,EventDispatcher, Twig,Form }'
348+ resource :' ../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber, Twig,Security }'
341349
342350 ..code-block ::xml
343351
@@ -352,7 +360,7 @@ the new ``Updates`` sub-directory:
352360<!-- ...-->
353361
354362<!-- 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 }" />
356364 </services >
357365 </container >
358366
@@ -370,7 +378,7 @@ Now, you can use the service immediately::
370378 }
371379
372380Thanks to autowiring and your type-hints in ``__construct() ``, the container creates
373- the ``SiteUpdateManager `` object and passes it the correctarguments . In most cases,
381+ the ``SiteUpdateManager `` object and passes it the correctargument . In most cases,
374382this works perfectly.
375383
376384Manually Wiring Arguments
@@ -428,7 +436,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428436
429437# same as before
430438AppBundle\ :
431- resource :' ../../src/AppBundle/{Service,Updates}'
439+ resource :' ../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security }'
432440
433441# explicitly configure the service
434442AppBundle\Updates\SiteUpdateManager :
@@ -448,7 +456,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
448456<!-- ...-->
449457
450458<!-- 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 }" />
452460
453461<!-- Explicitly configure the service-->
454462 <service id =" AppBundle\Updates\SiteUpdateManager" >
@@ -526,6 +534,8 @@ and reference it with the ``%parameter_name%`` syntax:
526534
527535 ..code-block ::php
528536
537+ // app/config/services.php
538+ use AppBundle\Updates\SiteUpdateManager;
529539 $container->setParameter('admin_email', 'manager@example.com');
530540
531541 $container->autowire(SiteUpdateManager::class)
@@ -659,7 +669,7 @@ The autoconfigure Option
659669Above, we've set ``autoconfigure: true `` in the ``_defaults `` section so that it
660670applies to all services defined in that file. With this setting, the container will
661671automatically 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.
663673
664674For example, to create a Twig Extension, you need to create a class, register it
665675as 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`
702712 ->addTag('twig.extension');
703713
704714 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:
706717
707718..configuration-block ::
708719
@@ -716,7 +727,7 @@ to do is load your service from the ``Twig`` directory:
716727
717728# load your services from the Twig directory
718729AppBundle\ :
719- resource :' ../../src/AppBundle/{Service,EventDispatcher,Twig ,Form}'
730+ resource :' ../../src/AppBundle/{Service,Updates,Command ,Form,EventSubscriber,Twig,Security }'
720731
721732 ..code-block ::xml
722733
@@ -731,7 +742,7 @@ to do is load your service from the ``Twig`` directory:
731742 <defaults autowire =" true" autoconfigure =" true" />
732743
733744<!-- 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 }" />
735746 </services >
736747 </container >
737748