8000 minor #10491 Improved the docs of the instanceof config (javiereguiluz) · symfony/symfony-docs@ad91c78 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad91c78

Browse files
committed
minor #10491 Improved the docs of the instanceof config (javiereguiluz)
This PR was merged into the 3.4 branch. Discussion ---------- Improved the docs of the instanceof config This finishes #9221. Commits ------- 3d256c6 Improved the docs of the instanceof config
2 parents a92d44b + 3d256c6 commit ad91c78

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

service_container/tags.rst

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,61 @@ then some tags are automatically applied for you. That's true for the ``twig.ext
6363
tag: the container sees that your class extends ``AbstractExtension`` (or more accurately,
6464
that it implements ``ExtensionInterface``) and adds the tag for you.
6565

66-
.. tip::
66+
If you want to apply tags automatically for your own services, use the
67+
``_instanceof`` option::
6768

68-
To apply a tag to all your autoconfigured services extending a class or implementing an
69-
interface, call the :method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::registerForAutoconfiguration`
70-
method in an :doc:`extension </bundles/extension>` or from your kernel::
69+
.. configuration-block::
7170

72-
// app/AppKernel.php
73-
class AppKernel extends Kernel
74-
{
75-
// ...
71+
.. code-block:: yaml
7672
77-
protected function build(ContainerBuilder $container)
78-
{
79-
$container->registerForAutoconfiguration(CustomInterface::class)
80-
->addTag('app.custom_tag')
81-
;
82-
}
73+
# app/config/services.yml
74+
services:
75+
# this config only applies to the services created by this file
76+
_instanceof:
77+
# services whose classes are instances of CustomInterface will be tagged automatically
78+
AppBundle\Security\CustomInterface:
79+
tags: ['app.custom_tag']
80+
# ...
81+
82+
.. code-block:: xml
83+
84+
<?xml version="1.0" encoding="utf-8"?>
85+
<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">
86+
<services>
87+
<!-- this config only applies to the services created by this file -->
88+
<instanceof id="AppBundle\Security\CustomInterface" autowire="true">
89+
<!-- services whose classes are instances of CustomInterface will be tagged automatically -->
90+
<tag name="app.custom_tag" />
91+
</instanceof>
92+
</services>
93+
</container>
94+
95+
.. code-block:: php
96+
97+
use AppBundle\Security\CustomInterface;
98+
// ...
99+
100+
// services whose classes are instances of CustomInterface will be tagged automatically
101+
$container->registerForAutoconfiguration(CustomInterface::class)
102+
->addTag('app.custom_tag')
103+
->setAutowired(true);
104+
105+
For more advanced needs, you can define the automatic tags using the
106+
:method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::registerForAutoconfiguration`
107+
method in an :doc:`extension </bundles/extension>` or from your kernel::
108+
109+
// app/AppKernel.php
110+
class AppKernel extends Kernel
111+
{
112+
// ...
113+
114+
protected function build(ContainerBuilder $container)
115+
{
116+
$container->registerForAutoconfiguration(CustomInterface::class)
117+
->addTag('app.custom_tag')
118+
;
83119
}
120+
}
84121

85122
Creating custom Tags
86123
--------------------

0 commit comments

Comments
 (0)
0