@@ -270,14 +270,6 @@ looks up the value of each parameter and uses it in the service definition.
270
270
271
271
<argument type =" string" >http://symfony.com/?foo=%%s& bar=%%d</argument >
272
272
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
-
281
273
The purpose of parameters is to feed information into services. Of course
282
274
there was nothing wrong with defining the service without using any parameters.
283
275
Parameters, however, have several advantages:
@@ -762,6 +754,36 @@ Injecting the dependency by the setter method just needs a change of syntax:
762
754
and "setter injection". The Symfony2 service container also supports
763
755
"property injection".
764
756
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
+
765
787
Making References Optional
766
788
--------------------------
767
789
0 commit comments