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

Commitbee8dd1

Browse files
committed
Merge pull requestsymfony#2729 from WouterJ/issue_2671
Documented synthetic services
2 parentscdca626 +dd7c90b commitbee8dd1

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+
<serviceid="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)

[8]ページ先頭

©2009-2025 Movatter.jp