Skip to content

[#2956] Refactoring the scopes entry #2972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 81 additions & 14 deletions book/service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -754,28 +754,95 @@ Injecting the dependency by the setter method just needs a change of syntax:
and "setter injection". The Symfony2 service container also supports
"property injection".

.. _book-container-request-stack:

Injecting the Request
~~~~~~~~~~~~~~~~~~~~~

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

Almost all Symfony2 built-in services behave in the same way: a single
instance is created by the container which it returns whenever you get it or
when it is injected into another service. There is one exception in a standard
Symfony2 application: the ``request`` service.

If you try to inject the ``request`` into a service, you will probably receive
a
:class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
exception. That's because the ``request`` can **change** during the life-time
of a container (when a sub-request is created for instance).

As of Symfony 2.4, instead of injecting the ``request`` service, you should
inject the ``request_stack`` service instead and access the Request by calling
the ``getCurrentRequest()`` method. For earlier versions, or if you want to
understand this problem better, refer to the cookbook article
:doc:`/cookbook/service_container/scopes`.
the ``getCurrentRequest()`` method:

namespace Acme\HelloBundle\Newsletter;

use Symfony\Component\HttpFoundation\RequestStack;

class NewsletterManager
{
protected $requestStack;

public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

public function anyMethod()
{
$request = $this->requestStack->getCurrentRequest();
// ... do something with the request
}

// ...
}

Now, just inject the ``request_stack``, which behaves like any normal service:

.. configuration-block::

.. code-block:: yaml

# src/Acme/HelloBundle/Resources/config/services.yml
services:
newsletter_manager:
class: "Acme\HelloBundle\Newsletter\NewsletterManager"
arguments: ["@request_stack"]

.. code-block:: xml

<!-- src/Acme/HelloBundle/Resources/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service
id="newsletter_manager"
class="Acme\HelloBundle\Newsletter\NewsletterManager"
>
<argument type="service" id="request_stack"/>
</service>
</services>
</container>

.. code-block:: php

// src/Acme/HelloBundle/Resources/config/services.php
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

// ...
$container->setDefinition('newsletter_manager', new Definition(
'Acme\HelloBundle\Newsletter\NewsletterManager',
array(new Reference('request_stack'))
));

.. sidebar: Why not Inject the request Service?

Almost all Symfony2 built-in services behave in the same way: a single
instance is created by the container which it returns whenever you get it or
when it is injected into another service. There is one exception in a standard
Symfony2 application: the ``request`` service.

If you try to inject the ``request`` into a service, you will probably receive
a
:class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
exception. That's because the ``request`` can **change** during the life-time
of a container (when a sub-request is created for instance).


.. tip::

Expand Down
Loading