Skip to content

Commit bee8dd1

Browse files
committed
Merge pull request #2729 from WouterJ/issue_2671
Documented synthetic services
2 parents cdca626 + dd7c90b commit bee8dd1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

components/dependency_injection/advanced.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,52 @@ below) to access this service (via the alias).
5959

6060
Services are by default public.
6161

62+
Synthetic Services
63+
------------------
64+
65+
Synthetic services are services that are injected into the container instead
66+
of being created by the container.
67+
68+
For instance, the ``request`` service which is injected in the
69+
:method:`HttpKernel::handle() <Symfony\\Component\\HttpKernel\\HttpKernel::handle>`
70+
method when entering the request :doc:`scope </cookbook/service_container/scopes>`.
71+
The class does not exist when there is no request, so it can't be included in
72+
the container configuration. Also, the service should be different for every
73+
subrequest in the application.
74+
75+
To create a synthetic service, set ``synthetic`` to ``true``:
76+
77+
.. configuration-block::
78+
79+
.. code-block:: yaml
80+
81+
services:
82+
request:
83+
synthetic: true
84+
85+
.. code-block:: xml
86+
87+
<service id="request"
88+
synthetic="true" />
89+
90+
.. code-block:: php
91+
92+
use Symfony\Component\DependencyInjection\Definition;
93+
94+
// ...
95+
$container->setDefinition('request', new Definition())
96+
->setSynthetic(true);
97+
98+
As you see, only the ``synthetic`` option is set. All other options are only used
99+
to configure the container how a service is created by the container. As the
100+
service isn't created by the container, these options are omitted.
101+
102+
Now, you can inject the class by using
103+
:method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::set`::
104+
105+
// ...
106+
$container->set('request', new MyRequest(...));
107+
62108
Aliasing
63109
--------
64110

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy