问题描述
我想在Bundle / Resources / config中添加一个新的配置文件。我已尝试关注,但它不工作,因为它应该和我得到
我的档案:
MailbrokerMailDetailsExtension.php
<?php
命名空间Mailbroker \MailDetailsBundle\DependencyInjection;
使用Symfony \Component\DependencyInjection\ContainerBuilder;
使用Symfony \Component\Config\FileLocator;
使用Symfony\Component\HttpKernel\DependencyInjection\Extension;
使用Symfony\Component\DependencyInjection\Loader;
类MailbrokerMailDetailsExtension extends扩展
{
/ **
* {@inheritDoc}
* /
public function load ,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
命名空间Mailbroker \MailDetailsBundle\DependencyInjection;
使用Symfony \Component\Config\Definition\Builder\TreeBuilder;
使用Symfony \Component\Config\Definition\ConfigurationInterface;
类配置implements ConfigurationInterface
{
/ **
* {@inheritDoc}
* /
public function getConfigTreeBuilder $ b {
$ treeBuilder = new TreeBuilder();
$ rootNode = $ treeBuilder-> root('mailbroker_mail_details');
$ rootNode
- > children()
- > scalarNode('abc') - > end()
- > end b $ b;
return $ treeBuilder;
}
}
canonisers.yml
mailbroker_mail_details:
abc:123
$ b b
配置是正确的(当放置在app / config / config.yml它加载它应该),canonisers.yml是正确加载,但由于某些原因,我不能让它一起工作。感谢您的帮助!
好吧,我没有尝试,但您应该能够使用Yaml扩展canonisers.yml文件直接添加到配置。不推荐(绕过应用程序缓存),但它可能工作:
使用Symfony \Component\Yaml\Yaml;
类MailbrokerMailDetailsExtension extends扩展
{
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 = > processConfiguration($ configuration,$ configs);
....
您可能需要添加到app / config / config.yml
mailbroker_mail_details:〜
只是为了得到错误信息。
它工作。
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
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!
解决方案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.
这篇关于Symfony 2加载自定义配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!