@@ -59,6 +59,52 @@ below) to access this service (via the alias).
59
59
60
60
Services are by default public.
61
61
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
+
62
108
Aliasing
63
109
--------
64
110
0 commit comments