1

I have defined in app/config.yml sample:

module_my_module
   key: value1
   key2: value2

And a bundle with DependecyInjection Configuration.php:

<?php

namespace Modules\Bundle\MyModuleBundle\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('module_configuration');
        $rootNode->children()
                ->scalarNode('key')->defaultValue('')->end()
                ->scalarNode('key2')->defaultValue('')->end()
            ->end()
            ;

        return $treeBuilder;
    }
}

A problem is how to call config.Bundle/Command class:

namespace Modules\Bundle\MyBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

//use \Modules\Bundle\NodeSocketBundle\DependencyInjection as DependencyInjection;

class MyCommand extends ContainerAwareCommand
{

    protected function execute(InputInterface $input, OutputInterface $output)
    {
    $this->getParams("key.val"); // GEt a key from config.yml
    }

}

Config.yml:

module_configuration
     key:  val
1
  • In a ContainerAwareCommand you can access to params as $this->getContainer()->getParameter("param_name") Commented Jan 15, 2015 at 11:14

1 Answer 1

1

You must inject your params in the Container in the Extension Bundle Class definition.

In your case you must have something similar to:

<?php

namespace Modules\Bundle\MyModuleBundle\DependencyInjection;


class MyModuleExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('...');

        $container->setParameter('my_first_key', $config['key']);
        $container->setParameter('my_second_key', $config['key2']);

    }
}

Then you can access to the value, in your Commandclass as:

class MyCommand extends ContainerAwareCommand
{

    protected function execute(InputInterface $input, OutputInterface $output)
    {
    $this->getContainer()->getParameter("my_first_key");
    }

}

Hope this help

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.