@@ -63,24 +63,61 @@ then some tags are automatically applied for you. That's true for the ``twig.ext
63
63
tag: the container sees that your class extends ``AbstractExtension `` (or more accurately,
64
64
that it implements ``ExtensionInterface ``) and adds the tag for you.
65
65
66
- .. tip ::
66
+ If you want to apply tags automatically for your own services, use the
67
+ ``_instanceof `` option::
67
68
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 ::
71
70
72
- // app/AppKernel.php
73
- class AppKernel extends Kernel
74
- {
75
- // ...
71
+ .. code-block :: yaml
76
72
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
+ ;
83
119
}
120
+ }
84
121
85
122
Creating custom Tags
86
123
--------------------
0 commit comments