New in Symfony 6.1: Simpler Bundle Extension and Configuration

Contributed by
Yonel Ceruto

in #43701.

Symfony bundles provide ready-to-use features to Symfony applications. In some
cases, bundles include their own configuration and can even
extend the application configuration adding new options.

These extension and configuration features are implemented via dedicated classes
that extend/implement other Symfony classes. This process is well-known and not
complicated, but it’s a bit verbose and cumbersome. That’s why in Symfony 6.1
we’re introducing a simpler way to configure and extend bundles.

This is the biggest change we ever made to the bundle system and you’ll
love it. In Symfony 6.1, you can define the configuration and extension of a
bundle directly in the main bundle class, without creating any other classes.

Let’s consider a Foo bundle that defines the following main bundle class:

namespace AcmeBundleFooBundle;

use SymfonyComponentHttpKernelBundleBundle;

class FooBundle extends Bundle

First, you need to change its base class to the new AbstractBundle:

// …
use SymfonyComponentHttpKernelBundleAbstractBundle;

class FooBundle extends AbstractBundle

Now, do you want to define semantic configuration for this bundle? Forget about
the Configuration class, the TreeBuilder object, the „extension alias“,
etc. Just define a configure() method in your bundle class:

class FooBundle extends AbstractBundle
// …

public function configure(DefinitionConfigurator $definition): void
// loads config definition from a file

// loads config definition from multiple files (when it’s too long you can split it)

// if the configuration is short, consider adding it in this class

The root key of your bundle configuration is automatically determined from your
bundle name (for FooBundle it would be foo). If you want to change it,
now you can simply define the following property in the bundle class:

class FooBundle extends AbstractBundle
protected string $extensionAlias = ‚acme‘;

// …

Finally, if you want to configure your bundle extension or to append/prepend
configuration options in the application, you no longer need to define an
Extension class, use the XmlFileLoader and FileLocator to load
configuration files, etc. Define the loadExtension() and/or
prependExtension() methods in your bundle and that’s all:

class FooBundle extends AbstractBundle
// …

// $config is the bundle Configuration that you usually process in
// ExtensionInterface::load() but already merged and processed
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
$container->parameters()->set(‚foo‘, $config[‚foo‘]);

if (‚bar‘ === $config[‚foo‘]) {

public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
// prepend some config option
$builder->prependExtensionConfig(‚framework‘, [
‚cache‘ => [‚prefix_seed‘ => ‚foo/bar‘],

// append some config option
$container->extension(‚framework‘, [
‚cache‘ => [‚prefix_seed‘ => ‚foo/bar‘],

// append options defined in some file (using any config format)

We’re beyond excited about this new feature! We hope you like it too and start
using it as soon as you upgrade to Symfony 6.1.

Sponsor the Symfony project.

Symfony Blog
Read More

Latest News


PHP 8.2.10 released!

PHP 8.1.23 released!

PHP 8.0.30 released!

Generated by Feedzy