charly
charly

Reputation: 956

Symfony can't override parameters on a bundle

I currently have a bundle and I define its parameter from the /app/config/config.yml file

foo:
   facebook:
       page: 
           name: %facebook_page_name%

I then access it in my controller by:

$this->container->getParameter('facebook.page.name');

What I did to make it work for the moment is in the FooExtension is

public function load(array $configs, ContainerBuilder $container)
{
    $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
    $loader->load('config.yml');
    $loader->load('services.yml');

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

    if (isset($config['facebook'])) {
        if (isset($config['facebook']['page'])) {
            if (isset($config['facebook']['page']['name'])) {
                $container->setParameter('facebook.page.name', $config['facebook']['page']['name']);
            }
        }
    }
}

You'll agree with me that this is exhausting and painful. How can I make that automatic? I've searched everywhere but there's definitely something I'm missing. but what?

Upvotes: 0

Views: 2980

Answers (2)

fd8s0
fd8s0

Reputation: 1927

Well, that's the way it is, unless you want to set them explicitely as parameters in the config.

Though if it's a mandatory tree structure, and you are already validating it through the configuration class in the bundle, you don't really need to fiddle so much with conditionals and issets.

It's not really painful nor exhausting, far from it, I'm sad to differ.

Upvotes: 1

Carlos Granados
Carlos Granados

Reputation: 11351

If you just need to pass some parameters, nothing fancy like providing default values, arrays of parameters or checking the configuration, you can just set some parameters like this:

# app/config/config.yml
parameters:
    facebook.page.name:    %facebook_page_name%

and then you can get them in your controller using the same code:

$this->container->getParameter('facebook.page.name');

Upvotes: 1

Related Questions