我有一个捆绑包,在一段时间内运行良好。但是,我必须向其中添加一些自定义配置参数,因此我在包的 config.yml 中编写了一些行,如下所示:
# ...
acme_my_bundle:
special_params: ['param_1', 'param_2']
配置在捆绑包中定义Configuration
class:
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
我查了一下,但发现的大多数结果都不令人满意——我排除了搜索过程中出现的问题:
- 配置结构不当
- 捆绑包未在应用程序内核中注册
- 配置根节点名称与返回的名称不同
ACMEMyBundleExtension::getAlias()
我尝试调试引发异常的原因,发现当 YAML 文件加载器尝试验证我的配置文件时,容器没有扩展名:
var_dump($container->getExtensions()); // prints empty array - array(0) { }
它会导致验证失败并且none要显示的消息的一部分 - 没有可用的扩展。
我尝试调试$this->extensions
in ContainerBuilder::hasExtension()
由于某种原因,当为供应商捆绑包启动该方法时,该列表是完整的,但对于我的捆绑包来说是空的。看起来我的包中的某些内容仍然定义或注册不正确。
I changed the names off classes, etc. not to expose company code, excuse me for that if it causes confusion.
EDIT:我没有明确提及,但Extension
定义了类,并且在加载该类时发生异常 - 正如我上面所写的:
当 YAML 文件加载器尝试验证我的配置文件时
更清楚地说,这是我的Extension
class:
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');
}
}