The Dependency Injection Tags

The Dependency Injection Tags

Dependency Injection Tags are little strings that can be applied to a service to "flag" it to be used in some special way. For example, if you have a service that you would like to register as a listener to one of Symfony's core events, you can flag it with the kernel.event_listener tag.

You can learn a little bit more about "tags" by reading the "Tags" section of the Service Container chapter.

Below is information about all of the tags available inside Symfony. There may also be tags in other bundles you use that aren't listed here.

Tag Name Usage
assetic.asset Register an asset to the current asset manager
assetic.factory_worker Add a factory worker
assetic.filter Register a filter
assetic.formula_loader Add a formula loader to the current asset manager
assetic.formula_resource Adds a resource to the current asset manager
assetic.templating.php Remove this service if PHP templating is disabled
assetic.templating.twig Remove this service if Twig templating is disabled
console.command Add a command
data_collector Create a class that collects custom data for the profiler
doctrine.event_listener Add a Doctrine event listener
doctrine.event_subscriber Add a Doctrine event subscriber
form.type Create a custom form field type
form.type_extension Create a custom "form extension"
form.type_guesser Add your own logic for "form type guessing"
kernel.cache_clearer Register your service to be called during the cache clearing process
kernel.cache_warmer Register your service to be called during the cache warming process
kernel.event_listener Listen to different events/hooks in Symfony
kernel.event_subscriber To subscribe to a set of different events/hooks in Symfony
kernel.fragment_renderer Add new HTTP content rendering strategies
monolog.logger Logging with a custom logging channel
monolog.processor Add a custom processor for logging
routing.loader Register a custom service that loads routes
routing.expression_language_provider Register a provider for expression language functions in routing
security.expression_language_provider Register a provider for expression language functions in security
security.voter Add a custom voter to Symfony's authorization logic
security.remember_me_aware To allow remember me authentication
serializer.encoder Register a new encoder in the serializer service
serializer.normalizer Register a new normalizer in the serializer service
swiftmailer.default.plugin Register a custom SwiftMailer Plugin
templating.helper Make your service available in PHP templates
translation.loader Register a custom service that loads translations
translation.extractor Register a custom service that extracts translation messages from a file
translation.dumper Register a custom service that dumps translation messages
twig.extension Register a custom Twig Extension
twig.loader Register a custom service that loads Twig templates
validator.constraint_validator Create your own custom validation constraint
validator.initializer Register a service that initializes objects before validation

assetic.asset

Purpose: Register an asset with the current asset manager

assetic.factory_worker

Purpose: Add a factory worker

A Factory worker is a class implementing Assetic\Factory\Worker\WorkerInterface. Its process($asset) method is called for each asset after asset creation. You can modify an asset or even return a new one.

In order to add a new worker, first create a class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use Assetic\Asset\AssetInterface;
use Assetic\Factory\Worker\WorkerInterface;

class MyWorker implements WorkerInterface
{
    public function process(AssetInterface $asset)
    {
        // ... change $asset or return a new one
    }

}

And then register it as a tagged service:

  • YAML
    1
    2
    3
    4
    5
    services:
        acme.my_worker:
            class: MyWorker
            tags:
                - { name: assetic.factory_worker }
    
  • 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="acme.my_worker" class="MyWorker>
                <tag name="assetic.factory_worker" />
            </service>
        </services>
    </container>
  • PHP
    1
    2
    3
    4
    $container
        ->register('acme.my_worker', 'MyWorker')
        ->addTag('assetic.factory_worker')
    ;
    

assetic.filter

Purpose: Register a filter

AsseticBundle uses this tag to register common filters. You can also use this tag to register your own filters.

First, you need to create a filter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
use Assetic\Asset\AssetInterface;
use Assetic\Filter\FilterInterface;

class MyFilter implements FilterInterface
{
    public function filterLoad(AssetInterface $asset)
    {
        $asset->setContent('alert("yo");' . $asset->getContent());
    }

    public function filterDump(AssetInterface $asset)
    {
        // ...
    }
}

Second, define a service:

  • YAML
    1
    2
    3
    4
    5
    services:
        acme.my_filter:
            class: MyFilter
            tags:
                - { name: assetic.filter, alias: my_filter }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?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="acme.my_filter" class="MyFilter">
                <tag name="assetic.filter" alias="my_filter" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('acme.my_filter', 'MyFilter')
        ->addTag('assetic.filter', array('alias' => 'my_filter'))
    ;
    

Finally, apply the filter:

1
2
3
4
5
6
{% javascripts
    '@AcmeBaseBundle/Resources/public/js/global.js'
    filter='my_filter'
%}
    <script src="{{ asset_url }}"></script>
{% endjavascripts %}

You can also apply your filter via the assetic.filters.my_filter.apply_to config option as it's described here: How to Apply an Assetic Filter to a specific File Extension. In order to do that, you must define your filter service in a separate xml config file and point to this file's path via the assetic.filters.my_filter.resource configuration key.

assetic.formula_loader

Purpose: Add a formula loader to the current asset manager

A Formula loader is a class implementing Assetic\\Factory\Loader\\FormulaLoaderInterface interface. This class is responsible for loading assets from a particular kind of resources (for instance, twig template). Assetic ships loaders for PHP and Twig templates.

An alias attribute defines the name of the loader.

assetic.formula_resource

Purpose: Adds a resource to the current asset manager

A resource is something formulae can be loaded from. For instance, Twig templates are resources.

assetic.templating.php

Purpose: Remove this service if PHP templating is disabled

The tagged service will be removed from the container if the framework.templating.engines config section does not contain php.

assetic.templating.twig

Purpose: Remove this service if Twig templating is disabled

The tagged service will be removed from the container if framework.templating.engines config section does not contain twig.

console.command

2.4Support for registering commands in the service container was introduced in Symfony 2.4.

Purpose: Add a command to the application

For details on registering your own commands in the service container, read the cookbook article.

data_collector

Purpose: Create a class that collects custom data for the profiler

For details on creating your own custom data collection, read the cookbook article: How to Create a custom Data Collector.

doctrine.event_listener

Purpose: Add a Doctrine event listener

For details on creating Doctrine event listeners, read the cookbook article: How to Register Event Listeners and Subscribers.

doctrine.event_subscriber

Purpose: Add a Doctrine event subscriber

For details on creating Doctrine event subscribers, read the cookbook article: How to Register Event Listeners and Subscribers.

form.type

Purpose: Create a custom form field type

For details on creating your own custom form type, read the cookbook article: How to Create a Custom Form Field Type.

form.type_extension

Purpose: Create a custom "form extension"

Form type extensions are a way for you took "hook into" the creation of any field in your form. For example, the addition of the CSRF token is done via a form type extension (FormTypeCsrfExtension).

A form type extension can modify any part of any field in your form. To create a form type extension, first create a class that implements the FormTypeExtensionInterface interface. For simplicity, you'll often extend an AbstractTypeExtension class instead of the interface directly:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// src/Acme/MainBundle/Form/Type/MyFormTypeExtension.php
namespace Acme\MainBundle\Form\Type;

use Symfony\Component\Form\AbstractTypeExtension;

class MyFormTypeExtension extends AbstractTypeExtension
{
    // ... fill in whatever methods you want to override
    // like buildForm(), buildView(), finishView(), setDefaultOptions()
}

In order for Symfony to know about your form extension and use it, give it the form.type_extension tag:

  • YAML
    1
    2
    3
    4
    5
    services:
        main.form.type.my_form_type_extension:
            class: Acme\MainBundle\Form\Type\MyFormTypeExtension
            tags:
                - { name: form.type_extension, alias: field }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="main.form.type.my_form_type_extension"
                class="Acme\MainBundle\Form\Type\MyFormTypeExtension">
    
                <tag name="form.type_extension" alias="field" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('main.form.type.my_form_type_extension', 'Acme\MainBundle\Form\Type\MyFormTypeExtension')
        ->addTag('form.type_extension', array('alias' => 'field'))
    ;
    

The alias key of the tag is the type of field that this extension should be applied to. For example, to apply the extension to any form/field, use the "form" value.

form.type_guesser

Purpose: Add your own logic for "form type guessing"

This tag allows you to add your own logic to the Form Guessing process. By default, form guessing is done by "guessers" based on the validation metadata and Doctrine metadata (if you're using Doctrine) or Propel metadata (if you're using Propel).

See also

For information on how to create your own type guesser, see Creating a custom Type Guesser.

kernel.cache_clearer

Purpose: Register your service to be called during the cache clearing process

Cache clearing occurs whenever you call cache:clear command. If your bundle caches files, you should add custom cache clearer for clearing those files during the cache clearing process.

In order to register your custom cache clearer, first you must create a service class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// src/Acme/MainBundle/Cache/MyClearer.php
namespace Acme\MainBundle\Cache;

use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;

class MyClearer implements CacheClearerInterface
{
    public function clear($cacheDir)
    {
        // clear your cache
    }

}

Then register this class and tag it with kernel.cache_clearer:

  • YAML
    1
    2
    3
    4
    5
    services:
        my_cache_clearer:
            class: Acme\MainBundle\Cache\MyClearer
            tags:
                - { name: kernel.cache_clearer }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?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="my_cache_clearer" class="Acme\MainBundle\Cache\MyClearer">
                <tag name="kernel.cache_clearer" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('my_cache_clearer', 'Acme\MainBundle\Cache\MyClearer')
        ->addTag('kernel.cache_clearer')
    ;
    

kernel.cache_warmer

Purpose: Register your service to be called during the cache warming process

Cache warming occurs whenever you run the cache:warmup or cache:clear task (unless you pass --no-warmup to cache:clear). It is also run when handling the request, if it wasn't done by one of the commands yet. The purpose is to initialize any cache that will be needed by the application and prevent the first user from any significant "cache hit" where the cache is generated dynamically.

To register your own cache warmer, first create a service that implements the CacheWarmerInterface interface:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// src/Acme/MainBundle/Cache/MyCustomWarmer.php
namespace Acme\MainBundle\Cache;

use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

class MyCustomWarmer implements CacheWarmerInterface
{
    public function warmUp($cacheDir)
    {
        // ... do some sort of operations to "warm" your cache
    }

    public function isOptional()
    {
        return true;
    }
}

The isOptional method should return true if it's possible to use the application without calling this cache warmer. In Symfony, optional warmers are always executed by default (you can change this by using the --no-optional-warmers option when executing the command).

To register your warmer with Symfony, give it the kernel.cache_warmer tag:

  • YAML
    1
    2
    3
    4
    5
    services:
        main.warmer.my_custom_warmer:
            class: Acme\MainBundle\Cache\MyCustomWarmer
            tags:
                - { name: kernel.cache_warmer, priority: 0 }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?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="main.warmer.my_custom_warmer" class="Acme\MainBundle\Cache\MyCustomWarmer">
                <tag name="kernel.cache_warmer" priority="0" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('main.warmer.my_custom_warmer', 'Acme\MainBundle\Cache\MyCustomWarmer')
        ->addTag('kernel.cache_warmer', array('priority' => 0))
    ;
    

Note

The priority value is optional, and defaults to 0. The higher the priority, the sooner it gets executed.

Core Cache Warmers

Cache Warmer Class Name Priority
TemplatePathsCacheWarmer 20
RouterCacheWarmer 0
TemplateCacheCacheWarmer 0

kernel.event_listener

Purpose: To listen to different events/hooks in Symfony

This tag allows you to hook your own classes into Symfony's process at different points.

For a full example of this listener, read the How to Create an Event Listener cookbook entry.

Core Event Listener Reference

When adding your own listeners, it might be useful to know about the other core Symfony listeners and their priorities.

Note

All listeners listed here may not be listening depending on your environment, settings and bundles. Additionally, third-party bundles will bring in additional listeners not listed here.

kernel.request

Listener Class Name Priority
ProfilerListener 1024
TestSessionListener 192
SessionListener 128
RouterListener 32
LocaleListener 16
Firewall 8

kernel.controller

Listener Class Name Priority
RequestDataCollector 0

kernel.exception

Listener Class Name Priority
ProfilerListener 0
ExceptionListener -128

kernel.terminate

Listener Class Name Priority
EmailSenderListener 0

kernel.event_subscriber

Purpose: To subscribe to a set of different events/hooks in Symfony

To enable a custom subscriber, add it as a regular service in one of your configuration, and tag it with kernel.event_subscriber:

  • YAML
    1
    2
    3
    4
    5
    services:
        kernel.subscriber.your_subscriber_name:
            class: Fully\Qualified\Subscriber\Class\Name
            tags:
                - { name: kernel.event_subscriber }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="kernel.subscriber.your_subscriber_name"
                class="Fully\Qualified\Subscriber\Class\Name">
    
                <tag name="kernel.event_subscriber" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('kernel.subscriber.your_subscriber_name', 'Fully\Qualified\Subscriber\Class\Name')
        ->addTag('kernel.event_subscriber')
    ;
    

Note

Your service must implement the EventSubscriberInterface interface.

Note

If your service is created by a factory, you MUST correctly set the class parameter for this tag to work correctly.

kernel.fragment_renderer

Purpose: Add a new HTTP content rendering strategy

To add a new rendering strategy - in addition to the core strategies like EsiFragmentRenderer - create a class that implements FragmentRendererInterface, register it as a service, then tag it with kernel.fragment_renderer.

monolog.logger

Purpose: To use a custom logging channel with Monolog

Monolog allows you to share its handlers between several logging channels. The logger service uses the channel app but you can change the channel when injecting the logger in a service.

  • YAML
    1
    2
    3
    4
    5
    6
    services:
        my_service:
            class: Fully\Qualified\Loader\Class\Name
            arguments: ["@logger"]
            tags:
                - { name: monolog.logger, channel: acme }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <?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="my_service" class="Fully\Qualified\Loader\Class\Name">
                <argument type="service" id="logger" />
                <tag name="monolog.logger" channel="acme" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    $definition = new Definition('Fully\Qualified\Loader\Class\Name', array(new Reference('logger'));
    $definition->addTag('monolog.logger', array('channel' => 'acme'));
    $container->setDefinition('my_service', $definition);
    

Tip

If you use MonologBundle 2.4 or higher, you can configure custom channels in the configuration and retrieve the corresponding logger service from the service container directly (see Configure Additional Channels without Tagged Services).

monolog.processor

Purpose: Add a custom processor for logging

Monolog allows you to add processors in the logger or in the handlers to add extra data in the records. A processor receives the record as an argument and must return it after adding some extra data in the extra attribute of the record.

The built-in IntrospectionProcessor can be used to add the file, the line, the class and the method where the logger was triggered.

You can add a processor globally:

  • YAML
    1
    2
    3
    4
    5
    services:
        my_service:
            class: Monolog\Processor\IntrospectionProcessor
            tags:
                - { name: monolog.processor }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?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="my_service" class="Monolog\Processor\IntrospectionProcessor">
                <tag name="monolog.processor" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('my_service', 'Monolog\Processor\IntrospectionProcessor')
        ->addTag('monolog.processor')
    ;
    

Tip

If your service is not a callable (using __invoke) you can add the method attribute in the tag to use a specific method.

You can add also a processor for a specific handler by using the handler attribute:

  • YAML
    1
    2
    3
    4
    5
    services:
        my_service:
            class: Monolog\Processor\IntrospectionProcessor
            tags:
                - { name: monolog.processor, handler: firephp }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?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="my_service" class="Monolog\Processor\IntrospectionProcessor">
                <tag name="monolog.processor" handler="firephp" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('my_service', 'Monolog\Processor\IntrospectionProcessor')
        ->addTag('monolog.processor', array('handler' => 'firephp'))
    ;
    

You can also add a processor for a specific logging channel by using the channel attribute. This will register the processor only for the security logging channel used in the Security component:

  • YAML
    1
    2
    3
    4
    5
    services:
        my_service:
            class: Monolog\Processor\IntrospectionProcessor
            tags:
                - { name: monolog.processor, channel: security }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?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="my_service" class="Monolog\Processor\IntrospectionProcessor">
                <tag name="monolog.processor" channel="security" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('my_service', 'Monolog\Processor\IntrospectionProcessor')
        ->addTag('monolog.processor', array('channel' => 'security'))
    ;
    

Note

You cannot use both the handler and channel attributes for the same tag as handlers are shared between all channels.

routing.loader

Purpose: Register a custom service that loads routes

To enable a custom routing loader, add it as a regular service in one of your configuration, and tag it with routing.loader:

  • YAML
    1
    2
    3
    4
    5
    services:
        routing.loader.your_loader_name:
            class: Fully\Qualified\Loader\Class\Name
            tags:
                - { name: routing.loader }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="routing.loader.your_loader_name"
                class="Fully\Qualified\Loader\Class\Name">
    
                <tag name="routing.loader" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('routing.loader.your_loader_name', 'Fully\Qualified\Loader\Class\Name')
        ->addTag('routing.loader')
    ;
    

For more information, see How to Create a custom Route Loader.

routing.expression_language_provider

2.6The routing.expression_language_provider tag was introduced in Symfony 2.6.

Purpose: Register a provider for expression language functions in routing

This tag is used to automatically register expression function providers for the routing expression component. Using these providers, you can add custom functions to the routing expression language.

security.expression_language_provider

2.6The security.expression_language_provider tag was introduced in Symfony 2.6.

Purpose: Register a provider for expression language functions in security

This tag is used to automatically register expression function providers for the security expression component. Using these providers, you can add custom functions to the security expression language.

security.remember_me_aware

Purpose: To allow remember me authentication

This tag is used internally to allow remember-me authentication to work. If you have a custom authentication method where a user can be remember-me authenticated, then you may need to use this tag.

If your custom authentication factory extends AbstractFactory and your custom authentication listener extends AbstractAuthenticationListener, then your custom authentication listener will automatically have this tagged applied and it will function automatically.

security.voter

Purpose: To add a custom voter to Symfony's authorization logic

When you call isGranted on Symfony's authorization checker, a system of "voters" is used behind the scenes to determine if the user should have access. The security.voter tag allows you to add your own custom voter to that system.

For more information, read the cookbook article: How to Implement your own Voter to Blacklist IP Addresses.

serializer.encoder

Purpose: Register a new encoder in the serializer service

The class that's tagged should implement the EncoderInterface and DecoderInterface.

For more details, see How to Use the Serializer.

serializer.normalizer

Purpose: Register a new normalizer in the Serializer service

The class that's tagged should implement the NormalizerInterface and DenormalizerInterface.

For more details, see How to Use the Serializer.

swiftmailer.default.plugin

Purpose: Register a custom SwiftMailer Plugin

If you're using a custom SwiftMailer plugin (or want to create one), you can register it with SwiftMailer by creating a service for your plugin and tagging it with swiftmailer.default.plugin (it has no options).

Note

default in this tag is the name of the mailer. If you have multiple mailers configured or have changed the default mailer name for some reason, you should change it to the name of your mailer in order to use this tag.

A SwiftMailer plugin must implement the Swift_Events_EventListener interface. For more information on plugins, see SwiftMailer's Plugin Documentation.

Several SwiftMailer plugins are core to Symfony and can be activated via different configuration. For details, see SwiftmailerBundle Configuration ("swiftmailer").

templating.helper

Purpose: Make your service available in PHP templates

To enable a custom template helper, add it as a regular service in one of your configuration, tag it with templating.helper and define an alias attribute (the helper will be accessible via this alias in the templates):

  • YAML
    1
    2
    3
    4
    5
    services:
        templating.helper.your_helper_name:
            class: Fully\Qualified\Helper\Class\Name
            tags:
                - { name: templating.helper, alias: alias_name }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="templating.helper.your_helper_name"
                class="Fully\Qualified\Helper\Class\Name">
    
                <tag name="templating.helper" alias="alias_name" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('templating.helper.your_helper_name', 'Fully\Qualified\Helper\Class\Name')
        ->addTag('templating.helper', array('alias' => 'alias_name'))
    ;
    

translation.loader

Purpose: To register a custom service that loads translations

By default, translations are loaded from the filesystem in a variety of different formats (YAML, XLIFF, PHP, etc).

See also

Learn how to load custom formats in the components section.

Now, register your loader as a service and tag it with translation.loader:

  • YAML
    1
    2
    3
    4
    5
    services:
        main.translation.my_custom_loader:
            class: Acme\MainBundle\Translation\MyCustomLoader
            tags:
                - { name: translation.loader, alias: bin }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="main.translation.my_custom_loader"
                class="Acme\MainBundle\Translation\MyCustomLoader">
    
                <tag name="translation.loader" alias="bin" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('main.translation.my_custom_loader', 'Acme\MainBundle\Translation\MyCustomLoader')
        ->addTag('translation.loader', array('alias' => 'bin'))
    ;
    

The alias option is required and very important: it defines the file "suffix" that will be used for the resource files that use this loader. For example, suppose you have some custom bin format that you need to load. If you have a bin file that contains French translations for the messages domain, then you might have a file app/Resources/translations/messages.fr.bin.

When Symfony tries to load the bin file, it passes the path to your custom loader as the $resource argument. You can then perform any logic you need on that file in order to load your translations.

If you're loading translations from a database, you'll still need a resource file, but it might either be blank or contain a little bit of information about loading those resources from the database. The file is key to trigger the load method on your custom loader.

translation.extractor

Purpose: To register a custom service that extracts messages from a file

When executing the translation:update command, it uses extractors to extract translation messages from a file. By default, the Symfony framework has a TwigExtractor and a PhpExtractor, which help to find and extract translation keys from Twig templates and PHP files.

You can create your own extractor by creating a class that implements ExtractorInterface and tagging the service with translation.extractor. The tag has one required option: alias, which defines the name of the extractor:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// src/Acme/DemoBundle/Translation/FooExtractor.php
namespace Acme\DemoBundle\Translation;

use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;

class FooExtractor implements ExtractorInterface
{
    protected $prefix;

    /**
     * Extracts translation messages from a template directory to the catalogue.
     */
    public function extract($directory, MessageCatalogue $catalogue)
    {
        // ...
    }

    /**
     * Sets the prefix that should be used for new found messages.
     */
    public function setPrefix($prefix)
    {
        $this->prefix = $prefix;
    }
}
  • YAML
    1
    2
    3
    4
    5
    services:
        acme_demo.translation.extractor.foo:
            class: Acme\DemoBundle\Translation\FooExtractor
            tags:
                - { name: translation.extractor, alias: foo }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="acme_demo.translation.extractor.foo"
                class="Acme\DemoBundle\Translation\FooExtractor">
    
                <tag name="translation.extractor" alias="foo" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    $container->register(
        'acme_demo.translation.extractor.foo',
        'Acme\DemoBundle\Translation\FooExtractor'
    )
        ->addTag('translation.extractor', array('alias' => 'foo'));
    

translation.dumper

Purpose: To register a custom service that dumps messages to a file

After an Extractor has extracted all messages from the templates, the dumpers are executed to dump the messages to a translation file in a specific format.

Symfony already comes with many dumpers:

You can create your own dumper by extending FileDumper or implementing DumperInterface and tagging the service with translation.dumper. The tag has one option: alias This is the name that's used to determine which dumper should be used.

  • YAML
    1
    2
    3
    4
    5
    services:
        acme_demo.translation.dumper.json:
            class: Acme\DemoBundle\Translation\JsonFileDumper
            tags:
                - { name: translation.dumper, alias: json }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="acme_demo.translation.dumper.json"
                class="Acme\DemoBundle\Translation\JsonFileDumper">
    
                <tag name="translation.dumper" alias="json" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    $container->register(
        'acme_demo.translation.dumper.json',
        'Acme\DemoBundle\Translation\JsonFileDumper'
    )
        ->addTag('translation.dumper', array('alias' => 'json'));
    

See also

Learn how to dump to custom formats in the components section.

twig.extension

Purpose: To register a custom Twig Extension

To enable a Twig extension, add it as a regular service in one of your configuration, and tag it with twig.extension:

  • YAML
    1
    2
    3
    4
    5
    services:
        twig.extension.your_extension_name:
            class: Fully\Qualified\Extension\Class\Name
            tags:
                - { name: twig.extension }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="twig.extension.your_extension_name"
                class="Fully\Qualified\Extension\Class\Name">
    
                <tag name="twig.extension" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('twig.extension.your_extension_name', 'Fully\Qualified\Extension\Class\Name')
        ->addTag('twig.extension')
    ;
    

For information on how to create the actual Twig Extension class, see Twig's documentation on the topic or read the cookbook article: How to Write a custom Twig Extension.

Before writing your own extensions, have a look at the Twig official extension repository which already includes several useful extensions. For example Intl and its localizeddate filter that formats a date according to user's locale. These official Twig extensions also have to be added as regular services:

  • YAML
    1
    2
    3
    4
    5
    services:
        twig.extension.intl:
            class: Twig_Extensions_Extension_Intl
            tags:
                - { name: twig.extension }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?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="twig.extension.intl" class="Twig_Extensions_Extension_Intl">
                <tag name="twig.extension" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('twig.extension.intl', 'Twig_Extensions_Extension_Intl')
        ->addTag('twig.extension')
    ;
    

twig.loader

Purpose: Register a custom service that loads Twig templates

By default, Symfony uses only one Twig Loader - FilesystemLoader. If you need to load Twig templates from another resource, you can create a service for the new loader and tag it with twig.loader:

  • YAML
    1
    2
    3
    4
    5
    services:
        acme.demo_bundle.loader.some_twig_loader:
            class: Acme\DemoBundle\Loader\SomeTwigLoader
            tags:
                - { name: twig.loader }
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    <?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="acme.demo_bundle.loader.some_twig_loader"
                class="Acme\DemoBundle\Loader\SomeTwigLoader">
    
                <tag name="twig.loader" />
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    $container
        ->register('acme.demo_bundle.loader.some_twig_loader', 'Acme\DemoBundle\Loader\SomeTwigLoader')
        ->addTag('twig.loader')
    ;
    

validator.constraint_validator

Purpose: Create your own custom validation constraint

This tag allows you to create and register your own custom validation constraint. For more information, read the cookbook article: How to Create a custom Validation Constraint.

validator.initializer

Purpose: Register a service that initializes objects before validation

This tag provides a very uncommon piece of functionality that allows you to perform some sort of action on an object right before it's validated. For example, it's used by Doctrine to query for all of the lazily-loaded data on an object before it's validated. Without this, some data on a Doctrine entity would appear to be "missing" when validated, even though this is not really the case.

If you do need to use this tag, just make a new class that implements the ObjectInitializerInterface interface. Then, tag it with the validator.initializer tag (it has no options).

For an example, see the EntityInitializer class inside the Doctrine Bridge.

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License .