Symfony App Config in 2 steps

yceruto

Yonel Ceruto

Posted on May 22, 2024

Symfony App Config in 2 steps

Customizing your Symfony application often involves tweaking configurations. For instance, enabling CSRF protection requires editing the framework.yaml file like this:

framework:
    csrf_protection: true
Enter fullscreen mode Exit fullscreen mode

These configurations, stored in the config/packages directory, come from third-party packages known as "Bundles." Each bundle can add its own configuration, identified by an extension alias (e.g. framework) and its settings (e.g. csrf_protection: true).

But what if you want to add custom configurations for your own application? Do you need to create a bundle for that? Not at all. Let me show you how to add your own application configuration in just 2 simple steps.

Step 1: Create an Extension Class

First, create an extension class to define your configuration. This class extends the Symfony\Component\DependencyInjection\Extension\AbstractExtension class and implements the configure() method:

<?php
namespace App\Extension;

use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\AbstractExtension;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

final class AppExtension extends AbstractExtension
{
    public function configure(DefinitionConfigurator $definition): void
    {
        $definition->rootNode()
            ->children()
                ->booleanNode('some_toggle')->defaultFalse()->end()
            ->end()
        ;
    }

    public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
    {
        $container->parameters()
            ->set('app.some_toggle', $config['some_toggle'])
        ;
    }

    public function getAlias(): string
    {
        return 'app';
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, we've defined a configuration with a single boolean value called some_toggle (default is false). This is used later by the loadExtension() method to create a container parameter that can be injected into your services or controllers.

The getAlias() method is optional. By default, Symfony will guess the alias from the class name, so AppExtension becomes app. You only need to override it if you want to use a different extension alias, e.g. project_name instead of app.

Step 2: Register the Extension Class

Next, register this extension class in the container within your Kernel class build() method, so the container knows about it:

<?php

namespace App;

use App\Extension\AppExtension;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    protected function build(ContainerBuilder $container): void
    {
        $container->registerExtension(new AppExtension());
    }
}
Enter fullscreen mode Exit fullscreen mode

And that’s it! Now you can add your own application configuration in a new config/packages/app.yaml file:

app:
    some_toggle: true
Enter fullscreen mode Exit fullscreen mode

I hope this guide helps you add custom configurations to your Symfony applications. If you have any questions, feel free to ask in the comments below.

💖 💪 🙅 🚩
yceruto
Yonel Ceruto

Posted on May 22, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related