简体   繁体   中英

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?

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');

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM