8000 Merge pull request #2972 from symfony/scope-refactoring · symfony/symfony-docs@941a5cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 941a5cc

Browse files
committed
Merge pull request #2972 from symfony/scope-refactoring
[#2956] Refactoring the scopes entry
2 parents f4975fd + 3dcbbc2 commit 941a5cc

File tree

2 files changed

+232
-132
lines changed

2 files changed

+232
-132
lines changed

book/service_container.rst

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -754,28 +754,95 @@ Injecting the dependency by the setter method just needs a change of syntax:
754754
and "setter injection". The Symfony2 service container also supports
755755
"property injection".
756756

757+
.. _book-container-request-stack:
758+
757759
Injecting the Request
758760
~~~~~~~~~~~~~~~~~~~~~
759761

760762
.. versionadded:: 2.4
761763
The ``request_stack`` service was introduced in version 2.4.
762764

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-
774765
As of Symfony 2.4, instead of injecting the ``request`` service, you should
775766
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\HelloBundle\Newsletter;
770+
771+
use Symfony\Component\HttpFoundation\RequestStack;
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\HelloBundle\Newsletter\NewsletterManager"
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+
779846
780847
.. tip::
781848

0 commit comments

Comments
 (0)
0