N
N
Nikolai Egorov2019-03-11 21:35:46
symfony
Nikolai Egorov, 2019-03-11 21:35:46

How can I access the config block I added?

I understand that the config is written for the bundle, and without using the bundle, adding a new section to config/services.yaml will not work. Therefore, I wrote the config (adding a new section as I wanted), added the src/DependencyInjection/AppSettingsConfiguration.php and src/DependencyInjection/AppSettingsExtension.php classes, added a line to the config/bundles.php file to connect the bundle.
But how to use the configuration I wrote - I do not understand. Tritely I can not understand how to get access to the config I added. DUMP() in src/DependencyInjection/AppSettingsExtension.php displays the tree I added as an array, no errors are thrown. But how, for example, to get access in the controller, or in the service, or in the twig...
Please help!

class AppSettingsExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new AppSettingsConfiguration();
        $config = $this->processConfiguration($configuration, $configs);

        dump($config);
    }
}

class AppSettingsConfiguration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('app_settings');

        $rootNode
            ->children()
                ->arrayNode('menuItems')
                    ->children()
                        ->arrayNode('mappings')
                            ->useAttributeAsKey('name')
                            ->arrayPrototype()
                            ->children()
                                ->scalarNode('class')->end()
                                ->scalarNode('type')->end()
                                ->scalarNode('trans')->end()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }
}

app_settings:
    menuItems:
        mappings:
            MenuItemCategory:
                class: 'App\Entity\MenuItemCategory'
                type: 'App\Form\Entity\MenuItemCategoryType'
                trans: 'label.menuItem_category_link'
            MenuItemPage:
                class: 'App\Entity\MenuItemPage'
                type: 'App\Form\Entity\MenuItemPageType'
                trans: 'label.menuItem_page_link'
            MenuItemArticle:
                class: 'App\Entity\MenuItemArticle'
                type: 'App\Form\Entity\MenuItemArticleType'
                trans: 'label.menuItem_article_link'
            MenuItemSimple:
                class: 'App\Entity\MenuItemSimple'
                type: 'App\Form\Entity\MenuItemSimpleType'
                trans: 'label.menuItem_simple_link'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2019-03-11
@nickicool

In Extension bundle, get the value from the config and put it in the container (it is available as the second parameter in the method load()):
And in the controller/any other service, pass these values ​​to the constructor:

arguments:
 - 'your.service.name.mapping'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question