Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improved the docs of the instanceof config#10491

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
javiereguiluz merged 1 commit intosymfony:3.4fromjaviereguiluz:finish_9221
Oct 15, 2018
Merged
Changes fromall 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
65 changes: 51 additions & 14 deletionsservice_container/tags.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,24 +63,61 @@ then some tags are automatically applied for you. That's true for the ``twig.ext
tag: the container sees that your class extends ``AbstractExtension`` (or more accurately,
that it implements ``ExtensionInterface``) and adds the tag for you.

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

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

// app/AppKernel.php
class AppKernel extends Kernel
{
// ...
.. code-block:: yaml

protected function build(ContainerBuilder $container)
{
$container->registerForAutoconfiguration(CustomInterface::class)
->addTag('app.custom_tag')
;
}
# app/config/services.yml
services:
# this config only applies to the services created by this file
_instanceof:
# services whose classes are instances of CustomInterface will be tagged automatically
AppBundle\Security\CustomInterface:
tags: ['app.custom_tag']
# ...

.. code-block:: 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>
<!-- this config only applies to the services created by this file -->
<instanceof id="AppBundle\Security\CustomInterface" autowire="true">
<!-- services whose classes are instances of CustomInterface will be tagged automatically -->
<tag name="app.custom_tag" />
</instanceof>
</services>
</container>

.. code-block:: php

use AppBundle\Security\CustomInterface;
// ...

// services whose classes are instances of CustomInterface will be tagged automatically
$container->registerForAutoconfiguration(CustomInterface::class)
->addTag('app.custom_tag')
->setAutowired(true);

For more advanced needs, you can define the automatic tags using the
:method:`Symfony\\Component\\DependencyInjection\\ContainerBuilder::registerForAutoconfiguration`
method in an :doc:`extension </bundles/extension>` or from your kernel::

// app/AppKernel.php
class AppKernel extends Kernel
{
// ...

protected function build(ContainerBuilder $container)
{
$container->registerForAutoconfiguration(CustomInterface::class)
->addTag('app.custom_tag')
;
}
}

Creating custom Tags
--------------------
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp