Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 12c4944

Browse filesBrowse files
committed
Tweaks after amazing review from @GuilhemN and @xabbuh
1 parent cac3c6c commit 12c4944
Copy full SHA for 12c4944

File tree

3 files changed

+51
-34
lines changed
Filter options

3 files changed

+51
-34
lines changed

‎controller.rst

Copy file name to clipboardExpand all lines: controller.rst
+21-15Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ and many others that you'll learn about next.
147147
You can extend either ``Controller`` or ``AbstractController``. The difference
148148
is that when you extend ``AbstractController``, you can't access services directly
149149
via ``$this->get()`` or ``$this->container->get()``. This forces you to write
150-
more robust code to access services, but if you're not use, use ``Controller``.
150+
more robust code to access services. But if you *do* need direct access to the
151+
container, using ``Controller`` is fine.
151152

152153
.. versionadded:: 3.3
153154
The ``AbstractController`` class was added in Symfony 3.3.
@@ -238,19 +239,19 @@ The Symfony templating system and Twig are explained more in the
238239
.. _controller-accessing-services:
239240
.. _accessing-other-services:
240241

241-
Fetching Services as Arguments
242-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
242+
Fetching Services as Controller Arguments
243+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243244

244245
.. versionadded:: 3.3
245-
The ability to type-hint an argument in order to receive a service was added
246-
in Symfony 3.3.
246+
The ability to type-hint a controller argument in order to receive a service
247+
was added in Symfony 3.3.
247248

248249
Symfony comes *packed* with a lot of useful objects, called :doc:`services </service_container>`.
249250
These are used for rendering templates, sending emails, querying the database and
250251
any other "work" you can think of.
251252

252-
If you need a service, just type-hint an argument with its class (or interface) name.
253-
Symfony will automatically pass you the service you need::
253+
If you need a service in a controller, just type-hint an argument with its class
254+
(or interface) name. Symfony will automatically pass you the service you need::
254255

255256
use Psr\Log\LoggerInterface
256257
// ...
@@ -266,16 +267,15 @@ Symfony will automatically pass you the service you need::
266267

267268
Awesome!
268269

269-
What other services exist? Each page of the documentation will reveal more and more
270-
services you can use. To list *all* services, use the ``debug:container`` console
270+
What other services can you type-hint? To see them, use the ``debug:container`` console
271271
command:
272272

273273
.. code-block:: terminal
274274
275-
$ php bin/console debug:container
275+
$ php bin/console debug:container --types
276276
277-
If need to control the *exact* value of an argument, you can override your controller's
278-
service config:
277+
If you need control over the *exact* value of an argument, you can override your
278+
controller's service config:
279279

280280
.. configuration-block::
281281

@@ -334,22 +334,26 @@ service config:
334334
])
335335
;
336336
337+
You can of course also use normal :ref:`constructor injection <services-constructor-injection>`
338+
in your controllers.
339+
337340
For more information about services, see the :doc:`/service_container` article.
338341

339342
.. note::
340343
If this isn't working, make sure your controller is registered as a service,
341-
:ref:`autoconfigured <services-autoconfigure>` and extends either
344+
is :ref:`autoconfigured <services-autoconfigure>` and extends either
342345
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` or
343346
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`. Or,
344-
you can tag your service manually with ``controller.service_arguments``.
347+
you can tag your service manually with ``controller.service_arguments``. All
348+
of this is done for you in a fresh Symfony install.
345349

346350
.. _accessing-other-services:
347351
.. _controller-access-services-directly:
348352

349353
Accessing the Container Directly
350354
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
351355

352-
If extending the base ``Controller`` class, you can access any Symfony service
356+
If you extend the base ``Controller`` class, you can access any Symfony service
353357
via the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::get`
354358
method. Here are several common services you might need::
355359

@@ -364,6 +368,8 @@ method. Here are several common services you might need::
364368

365369
If you receive an eror like:
366370

371+
.. code-block:: text
372+
367373
You have requested a non-existent service "my_service_id"
368374
369375
Check to make sure the service exists (use :ref:`debug:container <container-debug-container>`)

‎email.rst

Copy file name to clipboardExpand all lines: email.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ an email is pretty straightforward::
128128

129129
$mailer->send($message);
130130

131-
// or, you can also fetch the mailer service in this way
131+
// or, you can also fetch the mailer service this way
132132
// $this->get('mailer')->send($message);
133133

134134
return $this->render(...);

‎service_container.rst

Copy file name to clipboardExpand all lines: service_container.rst
+29-18Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Fetching and using Services
2525

2626
The moment you start a Symfony app, your container *already* contains many services.
2727
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
2929
service'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!)
157157
AppBundle\:
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 you set your service to be :ref:`public <container-public>`.
237+
However, this only works if you make 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+
239247
Injecting Services/Config into a Service
240248
----------------------------------------
241249

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``?
243251
Your service does *not* have access to the container directly, so you can't fetch
244252
it 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``
273281
type-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

278286
Be 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 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.
286294

287295
Handling Multiple Services
288296
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -337,7 +345,7 @@ the new ``Updates`` sub-directory:
337345
338346
# registers all classes in Services & Updates directories
339347
AppBundle\:
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

372380
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,
374382
this works perfectly.
375383

376384
Manually Wiring Arguments
@@ -428,7 +436,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428436
429437
# same as before
430438
AppBundle\:
431-
resource: '../../src/AppBundle/{Service,Updates}'
439+
resource: '../../src/AppBundle/{Service,Updates,Command,Form,EventSubscriber,Twig,Security}'
432440
433441
# explicitly configure the service
434442
AppBundle\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
659669
Above, we've set ``autoconfigure: true`` in the ``_defaults`` section so that it
660670
applies to all services defined in that file. With this setting, the container will
661671
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.
663673

664674
For example, to create a Twig Extension, you need to create a class, register it
665675
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`
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
718729
AppBundle\:
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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.