How to load, process and use custom parameters from Yaml configuration files in the DI Extension class?

I am trying to import the yaml configuration file into my application, following the documentation provided here http://symfony.com/doc/current/bundles/extension.html , but I always get an error message:

There is no extension capable of loading the configuration for the "application"

My file is here: config / packages / app.yaml and has the following structure:

app: list: model1: prop1: value1 prop2: value2 model2: ... 

Since this is a simple application, all files are in src/ . So I have src / DependencyInjection / AppExtension.php

 <?php namespace App\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader; class AppExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); } } 

and src / DependencyInjection / Configuration.php

 <?php namespace App\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('app'); // Node definition $rootNode ->children() ->arrayNode('list') ->useAttributeAsKey('name') ->requiresAtLeastOneElement() ->prototype('array') ->children() ->requiresAtLeastOneElement() ->prototype('scalar') ->end() ->end() ->end() ->end() ->end(); return $treeBuilder; } } 

I can not access my settings :(
Any idea?

+6
source share
1 answer

If you want to load a custom configuration file to process its parameters using the Extension class (as in the Symfony package extension, but without creating the package), eventually create and add one or more of them to the container (before it will be compiled ) you can register your Extension class manually in the configureContainer method contained in the Kernel.php file:

 protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader) { // to avoid the same error you need to put this line at the top // if your file is stored under "$this->getProjectDir().'/config'" directory $container->registerExtension(new YourAppExtensionClass()); // ----- rest of the code } 

Then you can use your parameters as usual by registering the compiler pass .

Hope this helps.

+11
source

Source: https://habr.com/ru/post/1274256/


All Articles