10

I want to add a new configuration file in Bundle/Resources/config. I've tried following http://symfony.com/doc/current/cookbook/bundles/extension.html , but it doesn't work as it should and I get

There is no extension able to load the configuration for "mailbroker_mail_details"

My files:

MailbrokerMailDetailsExtension.php

<?php

namespace Mailbroker\MailDetailsBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class MailbrokerMailDetailsExtension 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('services.yml');
        $loader->load('canonisers.yml');
    }

    public function getAlias()
    {
        return 'mailbroker_mail_details';
    }
}

Configuration.php

<?php

namespace Mailbroker\MailDetailsBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('mailbroker_mail_details');

        $rootNode
            ->children()
                ->scalarNode('abc')->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

canonisers.yml

mailbroker_mail_details:
    abc: 123

The Configuration is correct (when placed in app/config/config.yml it loads as it should), canonisers.yml is loaded correctly, but for some reason I can't make it work together. Thanks for your help!

2
  • The config object expects canonisers.yml to be already loaded and available in configs. Traditionally this would be under app/config and use imports: resource to load up. Your extension then load default values from Resources/config and merges in the app/config values.I am not explaining this very well. Take a look at some of the other framework bundles and see how they do the dependency injection stuff. You bundle would have two config files. One under Bundle\Resource\config to set the defaults and one under app/config to allow the user to override. Commented Mar 25, 2014 at 14:25
  • I understand what you mean, but still - is there a way which doesn't involve app/config (at least for now)? I want to allow fully default configuration, and I would like to not use parameters. Commented Mar 25, 2014 at 14:33

2 Answers 2

7

Well, I have not tried it but you should be able to use the Yaml extension to load in the canonisers.yml file directly and add it to configs. Not recommended (bypasses the application caching stuff) but it might work:

use Symfony\Component\Yaml\Yaml;

class MailbrokerMailDetailsExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $file = __DIR__.'/../Resources/config/canonisers.yml';
        $configs = array_merge($configs,Yaml::parse(file_get_contents($file));

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

Completely untested. You might need to add to app/config/config.yml

mailbroker_mail_details: ~

Just to get past the error message. Not sure.

Let me know if it works.

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

4 Comments

I have tried this and it works fine. Did not need to add data in the app/config for it to work.
So how exactly do you actually USE the config settings in your bundle or app?
@pthurmond - Kind of an old thread. Not sure how much of this is still applicable. As far as using the config settings, your best bet might be to look at some of the code under DependencyInjection in some common bundles such as FrameworkBundle, TwigBundle etc. Typically the config options are used to add services to the container.
It could be usefull to consider array_unshift($configs, Yaml::parse(file_get_contents($file))['mailbroker_mail_details'] instead of $configs = array_merge($configs,Yaml::parse(file_get_contents($file)); if you want to override the bundle "canonisers.yml" by the default app/config.yml file
3

Ok, so @Iltar from #symfony irc channel pointed me to cookbook: http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html

Long story short, PrependExtensionInterface with prepend method.

It was added since I last read through symfony book and cookbook, and it wasn't exactly googlable in this case, so I'll just leave the link here for other people.

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.