8000 Merge pull request #2956 from fabpot/request-stack · symfony/symfony-docs@3548a51 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3548a51

Browse files
committed
Merge pull request #2956 from fabpot/request-stack
added documentation for the new request_stack service
2 parents f1ab2ad + d9050a5 commit 3548a51

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

book/service_container.rst

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,6 @@ looks up the value of each parameter and uses it in the service definition.
270270
271271
<argument type="string">http://symfony.com/?foo=%%s&bar=%%d</argument>
272272
273-
.. caution::
274-
275-
You may receive a
276-
:class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
277-
when passing the ``request`` service as an argument. To understand this
278-
problem better and learn how to solve it, refer to the cookbook article
279-
:doc:`/cookbook/service_container/scopes`.
280-
281273
The purpose of parameters is to feed information into services. Of course
282274
there was nothing wrong with defining the service without using any parameters.
283275
Parameters, however, have several advantages:
@@ -762,6 +754,36 @@ Injecting the dependency by the setter method just needs a change of syntax:
762754
and "setter injection". The Symfony2 service container also supports
763755
"property injection".
764756

757+
Injecting the Request
758+
~~~~~~~~~~~~~~~~~~~~~
759+
760+
.. versionadded:: 2.4
761+
The ``request_stack`` service was introduced in version 2.4.
762+
763+
Almost all Symfony2 built-in services behave in the same way: a single
764+
instance is created by the container which it returns whenever you get it or
765+
when it is injected into another service. There is one exception in a standard
766+
Symfony2 application: the ``request`` service.
767+
768+
If you try to inject the ``request`` into a service, you will probably receive
769+
a
770+
:class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
771+
exception. That's because the ``request`` can **change** during the life-time
772+
of a container (when a sub-request is created for instance).
773+
774+
As of Symfony 2.4, instead of injecting the ``request`` service, you should
775+
inject the ``request_stack`` service instead and access the Request by calling
776+
the ``getCurrentRequest()`` method. For earlier versions, or if you want to
777+
understand this problem better, refer to the cookbook article
778+
:doc:`/cookbook/service_container/scopes`.
779+
780+
.. tip::
781+
782+
If you define a controller as a service then you can get the ``Request``
783+
object without injecting the container by having it passed in as an
784+
argument of your action method. See
785+
:ref:`book-controller-request-argument` for details.
786+
765787
Making References Optional
766788
--------------------------
767789

cookbook/service_container/scopes.rst

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ How to work with Scopes
66

77
This entry is all about scopes, a somewhat advanced topic related to the
88
:doc:`/book/service_container`. If you've ever gotten an error mentioning
9-
"scopes" when creating services, or need to create a service that depends
10-
on the ``request`` service, then this entry is for you.
9+
"scopes" when creating services, then this entry is for you.
10+
11+
.. note::
12+
13+
If you are trying to inject the ``request`` service, the simple solution
14+
is to inject the ``request_stack`` service instead and access the current
15+
Request by calling the ``getCurrentRequest()`` method.
1116

1217
Understanding Scopes
1318
--------------------
@@ -337,10 +342,3 @@ The service config for this class would look something like this:
337342

338343
Injecting the whole container into a service is generally not a good
339344
idea (only inject what you need).
340-
341-
.. tip::
342-
343-
If you define a controller as a service then you can get the ``Request``
344-
object without injecting the container by having it passed in as an
345-
argument of your action method. See
346-
:ref:`book-controller-request-argument` for details.

0 commit comments

Comments
 (0)
0