Symfony App Config in 2 steps
Tags: php, symfony, programming
Published: 2024-05-22 21:27:34

Customizing your Symfony application often involves tweaking configurations. For instance, enabling [CSRF](https://en.wikipedia.org/wiki/Cross-site_request_forgery) protection requires editing the `framework.yaml` file like this:

```yaml
framework:
    csrf_protection: true
```

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
<?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';
    }
}
```

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
<?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());
    }
}
```

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

```yaml
app:
    some_toggle: true
```

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.