Skip to content

Documented synthetic services #2729

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 1 commit into from
Jun 30, 2013
Merged
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
46 changes: 46 additions & 0 deletions components/dependency_injection/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,52 @@ below) to access this service (via the alias).

Services are by default public.

Synthetic Services
------------------

Synthetic services are services that are injected into the container instead
of being created by the container.

For instance, the ``request`` service which is injected in the
:method:`HttpKernel::handle() <Symfony\\Component\\HttpKernel\\HttpKernel::handle>`
method when entering the request :doc:`scope </cookbook/service_container/scopes>`.
The class does not exist when there is no request, so it can't be included in
the container configuration. Also, the service should be different for every
subrequest in the application.

To create a synthetic service, set ``synthetic`` to ``true``:

.. configuration-block::

.. code-block:: yaml

services:
request:
synthetic: true

.. code-block:: xml

<service id="request"
synthetic="true" />

.. code-block:: php

use Symfony\Component\DependencyInjection\Definition;

// ...
$container->setDefinition('request', new Definition())
->setSynthetic(true);

As you see, only the ``synthetic`` option is set. All other options are only used
to configure the container how a service is created by the container. As the
service isn't created by the container, these options are omitted.

Now, you can inject the class by using
:method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::set`::

// ...
$container->set('request', new MyRequest(...));

Aliasing
--------

Expand Down