Home > Net >  Symfony Bundle not creating bundle-configuration yaml file in project
Symfony Bundle not creating bundle-configuration yaml file in project

Time:01-17

A so far working bundle now needs its own configuration file inside the projects using the bundle, to manage bundle settings individually.

However, no matter which approach I use (the old one before Symfony 6.1 nor the new one extending AbstractBundle) there is - at no time - any new .yaml-File created inside the projects ./config/packages/ directory.


This is my code (the old style, prior to Symfony 6.1, extending Bundle):

Bundle Class

mycorpforms/src/MyCorpFormsBundle.php

<?php

namespace MyCorp\FormsBundle;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class MyCorpFormsBundle extends Bundle
{
   // empty
}

Configuration

mycorpforms/src/DependencyInjection/Configuration.php

<?php

namespace MyCorp\FormsBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('mycorp_forms');

        $treeBuilder->getRootNode()
            ->children()
                ->booleanNode('favorite_submenu_enabled')->defaultFalse()->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

Extension

mycorpforms/src/DependencyInjection/MyCorpFormsExtension.php

<?php
namespace MyCorp\FormsBundle\DependencyInjection;

use Knp\Bundle\SnappyBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class MyCorpFormsExtension extends Extension
{

    public function load(array $configs, ContainerBuilder $containerBuilder)
    {
        $loader = new YamlFileLoader(
            $containerBuilder,
            new FileLocator(__DIR__.'/../../config/packages')
        );
        $loader->load('mycorp_forms.yaml');

        $configuration = new Configuration();
        $processor = new Processor();
        $config = $processor->processConfiguration($configuration, $configs);

        $containerBuilder->setParameter('mycorp_forms.favorite_submenu_enabled', $config['favorite_submenu_enabled']);
    }
}

Yaml

Additionally I added the desired mycorp_forms.yaml inside the bundles ./config/packages/ dir.

This is the actual file required in the projects:

mycorpforms/config/packages/mycorp_forms.yaml

mycorp_forms:
    # Enable Favorite-Sub-Menu (Requires Knp-Snappy-Bundle !)
    favorite_submenu_enabled: false

The bundle installs flawlessly in any of my projects, however no mycorp_forms.yaml file is created. Obviously this requires symfony/flex which is so far required by the bundle itself.


Q: What do I miss here?
Q: How can this yaml-file automatically be added when the bundle is installed? I read the documentation up and down numerous times, but to be honest, I get more confused every time.


Thank you very much for any help or explanation!

CodePudding user response:

This requires Symfony Flex

To have a .yaml config file being generated/updated automatically, there is 2 possible ways:

  • Related