Skip to content

Add section about anonymous services #9909

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

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 36 additions & 0 deletions service_container/alias_private.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,42 @@ This means that when using the container directly, you can access the
# ...
app.mailer: '@app.phpmailer'

Anonymous Services
--------------------

.. note::

Anonymous services are currently only supported by the XML configuration format.

In some cases, you may want to limit access to a service outside of its
intended scope. This is useful when you want to prevent other services from
using the service as their own dependency. This can be achieved by creating an
*anonymous service*.

For Symfony, an anonymous service is nothing more than a service without a
identifying name which is defined directly in its use context. The following
example shows how an anonymous service can be injected into another service:

.. configuration-block::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need the configuration-block here when we only have one config format


.. code-block:: xml

<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="foo" class="AppBundle\Foo">
<argument type="service">
<service class="AppBundle\AnonymousBar" />
</argument>
</service>
</services>
</container>

Deprecating Services
--------------------

Expand Down