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
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';
}
}
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());
}
}
And that’s it! Now you can add your own application configuration in a new config/packages/app.yaml
file:
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.
Top comments (7)
Isn't this simpeler to do the same? Nice explenation though...
symfony.com/doc/current/best_pract...
I could imagine some cases where this could be useful, but maybe you could provide such a case? The example you provided could be solved much simpeler done with parameters: IMHO
If we take the example literally, yes; with the disadvantage that container parameters contain arbitrary values with no constraints or validation, unlike the advanced configuration I’m referring to here
You are absolutely right. I overlooked the validation part. The thing is that new Symfony developers reading this might implement it for a simple parameter value.
Anyway, I agree that using the approach above gives you much more control.
Nice post Yonel! Thanks for publishing it. We might need to update the Symfony Docs that explain how to create this semantic configuration in your app.
Interesting. I've only used this for reusable bundles but it can be useful to use in your app.
If you're using a lot of your own re-usable Symfony Bundles in your app, you might want to check out a library I made to make my life easier - packagist.org/packages/majermi4/fr... ;-)
Yeah, but how do bind these app config parameters to the _default bind parameter in other service yaml config file