我想在Bundle / Resources / config中添加一个新的配置文件。我已经尝试过关注http://symfony.com/doc/current/cookbook/bundles/extension.html,但它无法正常工作,我得到了
没有可以加载配置的扩展程序 “mailbroker_mail_details”
我的档案:
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
配置是正确的(当它放在app / config / config.yml中时它会加载它),canonisers.yml正确加载,但由于某种原因我不能让它一起工作。谢谢你的帮助!
答案 0 :(得分:7)
好吧,我没有尝试过,但您应该可以使用Yaml扩展名直接加载到canonisers.yml文件中并将其添加到配置中。不推荐(绕过应用程序缓存的东西)但它可能有效:
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);
....
完全未经测试。您可能需要添加到app / config / config.yml
mailbroker_mail_details: ~
只是为了通过错误消息。不确定。
让我知道它是否有效。
答案 1 :(得分:3)
好的,所以来自#symfony irc频道的@Iltar向我指出了食谱:http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html
长话短说,PrependExtensionInterface和prepend方法。
自从我上次阅读symfony书和食谱以来,它被添加了,在这种情况下它并不完全可以转让,所以我只是将链接留给其他人。