Skip to content

Commit abdcf9f

Browse files
committed
fixed markup
1 parent c2d0b99 commit abdcf9f

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

cookbook/service_container/scopes.rst

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ How to work with Scopes
77
This entry is all about scopes, a somewhat advanced topic related to the
88
:doc:`/book/service_container`. If you've ever gotten an error mentioning
99
"scopes" when creating services, or need to create a service that depends
10-
on the `request` service, then this entry is for you.
10+
on the ``request`` service, then this entry is for you.
1111

1212
Understanding Scopes
1313
--------------------
@@ -16,49 +16,49 @@ The scope of a service controls how long an instance of a service is used
1616
by the container. The Dependency Injection component provides two generic
1717
scopes:
1818

19-
- `container` (the default one): The same instance is used each time you
19+
- ``container`` (the default one): The same instance is used each time you
2020
request it from this container.
2121

22-
- `prototype`: A new instance is created each time you request the service.
22+
- ``prototype``: A new instance is created each time you request the service.
2323

24-
The FrameworkBundle also defines a third scope: `request`. This scope is
24+
The FrameworkBundle also defines a third scope: ``request``. This scope is
2525
tied to the request, meaning a new instance is created for each subrequest
2626
and is unavailable outside the request (for instance in the CLI).
2727

2828
Scopes add a constraint on the dependencies of a service: a service cannot
2929
depend on services from a narrower scope. For example, if you create a generic
30-
`my_foo` service, but try to inject the `request` component, you'll receive
30+
``my_foo`` service, but try to inject the ``request`` component, you'll receive
3131
a :class:`Symfony\\Component\\DependencyInjection\\Exception\\ScopeWideningInjectionException`
3232
when compiling the container. Read the sidebar below for more details.
3333

3434
.. sidebar:: Scopes and Dependencies
3535

36-
Imagine you've configured a `my_mailer` service. You haven't configured
37-
the scope of the service, so it defaults to `container`. In other words,
38-
every time you ask the container for the `my_mailer` service, you get
36+
Imagine you've configured a ``my_mailer`` service. You haven't configured
37+
the scope of the service, so it defaults to ``container``. In other words,
38+
every time you ask the container for the ``my_mailer`` service, you get
3939
the same object back. This is usually how you want your services to work.
4040

41-
Imagine, however, that you need the `request` service in your `my_mailer`
41+
Imagine, however, that you need the ``request`` service in your ``my_mailer``
4242
service, maybe because you're reading the URL of the current request.
4343
So, you add it as a constructor argument. Let's look at why this presents
4444
a problem:
4545

46-
* When requesting `my_mailer`, an instance of `my_mailer` (let's call
47-
it *MailerA*) is created and the `request` service (let's call it
46+
* When requesting ``my_mailer``, an instance of ``my_mailer`` (let's call
47+
it *MailerA*) is created and the ``request`` service (let's call it
4848
*RequestA*) is passed to it. Life is good!
4949

5050
* You've now made a subrequest in Symfony, which is a fancy way of saying
51-
that you've called, for example, the `{{ render(...) }}` Twig function,
52-
which executes another controller. Internally, the old `request` service
51+
that you've called, for example, the ``{{ render(...) }}`` Twig function,
52+
which executes another controller. Internally, the old ``request`` service
5353
(*RequestA*) is actually replaced by a new request instance (*RequestB*).
5454
This happens in the background, and it's totally normal.
5555

56-
* In your embedded controller, you once again ask for the `my_mailer`
57-
service. Since your service is in the `container` scope, the same
56+
* In your embedded controller, you once again ask for the ``my_mailer``
57+
service. Since your service is in the ``container`` scope, the same
5858
instance (*MailerA*) is just re-used. But here's the problem: the
5959
*MailerA* instance still contains the old *RequestA* object, which
6060
is now **not** the correct request object to have (*RequestB* is now
61-
the current `request` service). This is subtle, but the mis-match could
61+
the current ``request`` service). This is subtle, but the mis-match could
6262
cause major problems, which is why it's not allowed.
6363

6464
So, that's the reason *why* scopes exist, and how they can cause
@@ -101,20 +101,20 @@ The scope of a service is set in the definition of the service:
101101
new Definition('Acme\HelloBundle\Mail\GreetingCardManager')
102102
)->setScope('request');
103103
104-
If you don't specify the scope, it defaults to `container`, which is what
104+
If you don't specify the scope, it defaults to ``container``, which is what
105105
you want most of the time. Unless your service depends on another service
106-
that's scoped to a narrower scope (most commonly, the `request` service),
106+
that's scoped to a narrower scope (most commonly, the ``request`` service),
107107
you probably don't need to set the scope.
108108

109109
Using a Service from a narrower Scope
110110
-------------------------------------
111111

112112
If your service depends on a scoped service, the best solution is to put
113113
it in the same scope (or a narrower one). Usually, this means putting your
114-
new service in the `request` scope.
114+
new service in the ``request`` scope.
115115

116116
But this is not always possible (for instance, a twig extension must be in
117-
the `container` scope as the Twig environment needs it as a dependency).
117+
the ``container`` scope as the Twig environment needs it as a dependency).
118118
In these cases, you should pass the entire container into your service and
119119
retrieve your dependency from the container each time you need it to be sure
120120
you have the right instance::

0 commit comments

Comments
 (0)