@@ -754,28 +754,95 @@ Injecting the dependency by the setter method just needs a change of syntax:
754
754
and "setter injection". The Symfony2 service container also supports
755
755
"property injection".
756
756
757
+ .. _book-container-request-stack :
758
+
757
759
Injecting the Request
758
760
~~~~~~~~~~~~~~~~~~~~~
759
761
760
762
.. versionadded :: 2.4
761
763
The ``request_stack `` service was introduced in version 2.4.
762
764
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
765
As of Symfony 2.4, instead of injecting the ``request `` service, you should
775
766
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 `.
767
+ the ``getCurrentRequest() `` method:
768
+
769
+ namespace Acme\H elloBundle\N ewsletter;
770
+
771
+ use Symfony\C omponent\H ttpFoundation\R equestStack;
772
+
773
+ class NewsletterManager
774
+ {
775
+ protected $requestStack;
776
+
777
+ public function __construct(RequestStack $requestStack)
778
+ {
779
+ $this->requestStack = $requestStack;
780
+ }
781
+
782
+ public function anyMethod()
783
+ {
784
+ $request = $this->requestStack->getCurrentRequest();
785
+ // ... do something with the request
786
+ }
787
+
788
+ // ...
789
+ }
790
+
791
+ Now, just inject the ``request_stack ``, which behaves like any normal service:
792
+
793
+ .. configuration-block ::
794
+
795
+ .. code-block :: yaml
796
+
797
+ # src/Acme/HelloBundle/Resources/config/services.yml
798
+ services :
799
+ newsletter_manager :
800
+ class : " Acme\H elloBundle\N ewsletter\N ewsletterManager"
801
+ arguments : ["@request_stack"]
802
+
803
+ .. code-block :: xml
804
+
805
+ <!-- src/Acme/HelloBundle/Resources/config/services.xml -->
806
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
807
+ <container xmlns =" http://symfony.com/schema/dic/services"
808
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
809
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
810
+
811
+ <services >
812
+ <service
813
+ id =" newsletter_manager"
814
+ class =" Acme\HelloBundle\Newsletter\NewsletterManager"
815
+ >
816
+ <argument type =" service" id =" request_stack" />
817
+ </service >
818
+ </services >
819
+ </container >
820
+
821
+ .. code-block :: php
822
+
823
+ // src/Acme/HelloBundle/Resources/config/services.php
824
+ use Symfony\Component\DependencyInjection\Definition;
825
+ use Symfony\Component\DependencyInjection\Reference;
826
+
827
+ // ...
828
+ $container->setDefinition('newsletter_manager', new Definition(
829
+ 'Acme\HelloBundle\Newsletter\NewsletterManager',
830
+ array(new Reference('request_stack'))
831
+ ));
832
+
833
+ .. sidebar: Why not Inject the request Service?
834
+
835
+ Almost all Symfony2 built-in services behave in the same way: a single
836
+ instance is created by the container which it returns whenever you get it or
837
+ when it is injected into another service. There is one exception in a standard
838
+ Symfony2 application: the ``request`` service.
839
+
840
+ If you try to inject the ``request`` into a service, you will probably receive
841
+ a
842
+ :class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
843
+ exception. That's because the ``request`` can **change** during the life-time
844
+ of a container (when a sub-request is created for instance).
845
+
779
846
780
847
.. tip ::
781
848
0 commit comments