【发布时间】:2015-03-09 08:03:59
【问题描述】:
我有一个在一段时间内运行良好的捆绑包。但是,我必须为其添加一些自定义配置参数,所以我在包的 config.yml 中写了一些行,如下所示:
# ...
acme_my_bundle:
special_params: ['param_1', 'param_2']
配置在包的Configuration类中定义:
namespace ACME\MyBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface {
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder() {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('acme_my_bundle');
$rootNode
->children()
->arrayNode('special_params')
->end()
->end();
return $treeBuilder;
}
}
捆绑包已在AppKernel.php 中正确注册:
public function registerBundles() {
$bundles = array(
// ...
new ACME\MyBundle(),
// ...
);
// ...
return $bundles;
}
但是,当我尝试使用我的应用时,我收到了一个错误:
There is no extension able to load the configuration for "acme_my_bundle" (in (path_to_bundle)/MyBundle/DependencyInjection/../Resources/config/config.yml). Looked for namespace "acme_my_bundle", found none
我查了一下,但发现的大多数结果都不令人满意——我排除了搜索过程中出现的问题:
- 配置结构不正确
- bundle 未在应用内核中注册
- 配置根节点名与
ACMEMyBundleExtension::getAlias()返回的不同
我尝试调试引发异常的原因,发现当 YAML 文件加载器尝试验证我的配置文件时,容器没有扩展名:
var_dump($container->getExtensions()); // prints empty array - array(0) { }
这会导致验证失败并显示消息的 none 部分 - 没有可用的扩展。
我尝试在ContainerBuilder::hasExtension() 中调试$this->extensions,由于某种原因,当为供应商捆绑包启动该方法时列表已完成,但对于我的捆绑包是空的。看起来我的包中的某些内容仍然定义或注册不正确。
为了不暴露公司代码,我改了类名等等
编辑:我没有明确提到它,但是 当 YAML 文件加载器尝试验证我的配置文件时 为了更清楚,这是我的Extension 类是定义的,并且在加载时发生异常 - 正如我在上面写的那样:
Extension 类:namespace ACME\MyBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class ACMEMyBundleExtension 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'));
// The exception is thrown here
$loader->load('config.yml');
}
}
【问题讨论】:
标签: php symfony configuration