Symfony 容器在加载我的包时没有扩展
Symfony container has no extensions when loading my bundle
我有一个捆绑包在一段时间内运行良好。但是,我必须向它添加一些自定义配置参数,所以我在包的 config.yml 中写了一些行,如下所示:
# ...
acme_my_bundle:
special_params: ['param_1', 'param_2']
配置定义在bundle的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 部分 - 没有可用的扩展。
我尝试在 ContainerBuilder::hasExtension()
中调试 $this->extensions
并且出于某种原因,当为供应商捆绑包启动该方法时列表是完整的,但对于我的捆绑包是空的。看来我的包中的某些内容仍未正确定义或注册。
为了不暴露公司代码,我把名字改掉了classes等,如有混淆请见谅。
编辑: 我没有明确提到它,但是 Extension
class 被定义并且加载时发生异常 - 就像我在上面写了:
when the YAML file loader tries to validate my config file
为了更清楚,这是我的 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');
}
}
您错过了包扩展 class (ACME\MyBundle\DependencyInjection\ACMEMyExtension),如此处 http://symfony.com/doc/current/cookbook/bundles/extension.html. Cookbook entry for bundle configuration is here 所述。 config.yml 中的键值只能命名为 acme_my.
仅创建 Configuration
class 是不够的。您需要注册一个依赖注入扩展并在其中使用 Configuration
class。
阅读 How to Create Friendly Configuration for a Bundle 食谱中的更多信息:
[The Configuration] class can now be used in your load() method to merge configurations and force validation (e.g. if an additional option was passed, an exception will be thrown)
namespace Acme\MyBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AcmeMyBundleExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
// ...
}
}
根据约定命名您的扩展程序 class 将使其自动加载。更多关于在 Creating an Extension Class. You can also enable the extension manually, see Manually registering an extension class.
中创建 DIC 扩展 classes
检查 reader 在 ACME\MyBundle\DependencyInjection\Configuration
中的 $rootNode = $treeBuilder->root('BUNDLE_CONFIG_KEY');
配置。
BUNDLE_CONFIG_KEY
应该是:
- 有效(在
ACME\MyBundle\DependencyInjection\Configuration
和您的 config.yml
中相同
- 应用程序独有
另外请检查您是否以正确的方式定义了包配置——它应该添加到 app/config/*.yml
(全局配置文件之一)。也许您在其他自定义捆绑包配置文件中添加了 acme_my_bundle
配置?
我有一个捆绑包在一段时间内运行良好。但是,我必须向它添加一些自定义配置参数,所以我在包的 config.yml 中写了一些行,如下所示:
# ...
acme_my_bundle:
special_params: ['param_1', 'param_2']
配置定义在bundle的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 部分 - 没有可用的扩展。
我尝试在 ContainerBuilder::hasExtension()
中调试 $this->extensions
并且出于某种原因,当为供应商捆绑包启动该方法时列表是完整的,但对于我的捆绑包是空的。看来我的包中的某些内容仍未正确定义或注册。
为了不暴露公司代码,我把名字改掉了classes等,如有混淆请见谅。
编辑: 我没有明确提到它,但是 Extension
class 被定义并且加载时发生异常 - 就像我在上面写了:
when the YAML file loader tries to validate my config file
为了更清楚,这是我的 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');
}
}
您错过了包扩展 class (ACME\MyBundle\DependencyInjection\ACMEMyExtension),如此处 http://symfony.com/doc/current/cookbook/bundles/extension.html. Cookbook entry for bundle configuration is here 所述。 config.yml 中的键值只能命名为 acme_my.
仅创建 Configuration
class 是不够的。您需要注册一个依赖注入扩展并在其中使用 Configuration
class。
阅读 How to Create Friendly Configuration for a Bundle 食谱中的更多信息:
[The Configuration] class can now be used in your load() method to merge configurations and force validation (e.g. if an additional option was passed, an exception will be thrown)
namespace Acme\MyBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AcmeMyBundleExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
// ...
}
}
根据约定命名您的扩展程序 class 将使其自动加载。更多关于在 Creating an Extension Class. You can also enable the extension manually, see Manually registering an extension class.
中创建 DIC 扩展 classes检查 reader 在 ACME\MyBundle\DependencyInjection\Configuration
中的 $rootNode = $treeBuilder->root('BUNDLE_CONFIG_KEY');
配置。
BUNDLE_CONFIG_KEY
应该是:
- 有效(在
ACME\MyBundle\DependencyInjection\Configuration
和您的config.yml
中相同
- 应用程序独有
另外请检查您是否以正确的方式定义了包配置——它应该添加到 app/config/*.yml
(全局配置文件之一)。也许您在其他自定义捆绑包配置文件中添加了 acme_my_bundle
配置?