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 Acme\Bundle\FooBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class FooBundle extends Bundle { }

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

    // ...

use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

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
    $definition->import('../config/definition.php');

    // loads config definition from multiple files (when it's too long you can split it)
    $definition->import('../config/definition/*.php');

    // if the configuration is short, consider adding it in this class
    $definition->rootNode()
        ->children()
            ->scalarNode('foo')->defaultValue('bar')->end()
        ->end()
    ;
}

}

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']);
    $container->import('../config/services.php');

    if ('bar' === $config['foo']) {
        $container->services()->set(Parser::class);
    }
}

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)
    $container->import('../config/packages/cache.php');
}

}

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.

https://symfony.com/blog/new-in-symfony-6-1-simpler-bundle-extension-and-configuration?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | May 6, 2022, 9:20:16 AM


Login to add comment

Other posts in this group

SymfonyLive Paris 2025 : Reveal of workshop topics!

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

💻

Jan 30, 2025, 8:50:03 AM | Symfony
Get Symfony news on your favorite social network

Symfony has been active on X, Mastodon, and Bluesky for some time, but until recently, not all platforms received equal attention. Since Twitter (now X) was our first social network, all blog posts we

Jan 29, 2025, 2:20:10 PM | Symfony
SymfonyLive Berlin 2025: Demystify the magic of the Container

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

As we are now unveiling th

Jan 29, 2025, 2:20:10 PM | Symfony
Twig CVE-2025-24374: Missing output escaping for the null coalesce operator

Affected versions

Twig versions >=3.16.0,<3.19.0 are affected by this security issue.

The issue has been fixed in Twig 3.19.0.

Description

When using the null coalesce operator (??), output esc

Jan 29, 2025, 9:40:06 AM | Symfony
Symfony 6.4.18 released

Symfony 6.4.18 has just been released. Here is the list of the most important changes since 6.4.17:

bug #58889 [Serializer] Handle default context in Serializer (@Valmonzo)

bug #59631 [HttpClient

Jan 29, 2025, 9:40:05 AM | Symfony
Symfony 7.1.11 released

Symfony 7.1.11 has just been released. Here is the list of the most important changes since 7.1.10:

bug #58889 [Serializer] Handle default context in Serializer (@Valmonzo)

bug #59631 [HttpClient

Jan 29, 2025, 9:40:05 AM | Symfony
Symfony 7.2.3 released

Symfony 7.2.3 has just been released. Here is the list of the most important changes since 7.2.2:

bug #58889 [Serializer] Handle default context in Serializer (@Valmonzo)

bug #59631 [HttpClient]

Jan 29, 2025, 9:40:04 AM | Symfony