Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit3dcbbc2

Browse files
committed
[#2972] Clarifying how to use the request_stack with an example
1 parentd417f7b commit3dcbbc2

File tree

1 file changed

+79
-14
lines changed

1 file changed

+79
-14
lines changed

‎book/service_container.rst

Lines changed: 79 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -762,22 +762,87 @@ Injecting the Request
762762
..versionadded::2.4
763763
The ``request_stack`` service was introduced in version 2.4.
764764

765-
Almost all Symfony2 built-in services behave in the same way: a single
766-
instance is created by the container which it returns whenever you get it or
767-
when it is injected into another service. There is one exception in a standard
768-
Symfony2 application: the ``request`` service.
769-
770-
If you try to inject the ``request`` into a service, you will probably receive
771-
a
772-
:class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
773-
exception. That's because the ``request`` can **change** during the life-time
774-
of a container (when a sub-request is created for instance).
775-
776765
As of Symfony 2.4, instead of injecting the ``request`` service, you should
777766
inject the ``request_stack`` service instead and access the Request by calling
778-
the ``getCurrentRequest()`` method. For earlier versions, or if you want to
779-
understand this problem better, refer to the cookbook article
780-
: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+
<containerxmlns="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+
<argumenttype="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+
781846
782847
..tip::
783848

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp