Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

WARNING: You are browsing the documentation for Symfony 2.7 which is not maintained anymore. Consider upgrading your projects to Symfony 5.1.

How to Work with Compiler Passes in Bundles

2.7 version

How to Work with Compiler Passes in Bundles

Compiler passes give you an opportunity to manipulate other service definitions that have been registered with the service container. You can read about how to create them in the components section “Compiling the Container”. To register a compiler pass from a bundle you need to add it to the build method of the bundle definition class:

// src/Acme/MailerBundle/AcmeMailerBundle.php
namespace Acme\MailerBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

use Acme\MailerBundle\DependencyInjection\Compiler\CustomCompilerPass;

class AcmeMailerBundle extends Bundle
    public function build(ContainerBuilder $container)

        $container->addCompilerPass(new CustomCompilerPass());

One of the most common use-cases of compiler passes is to work with tagged services (read more about tags in “How to Work with Service Tags”). If you are using custom tags in a bundle then by convention, tag names consist of the name of the bundle (lowercase, underscores as separators), followed by a dot, and finally the “real” name. For example, if you want to introduce some sort of “transport” tag in your AcmeMailerBundle, you should call it acme_mailer.transport.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.