作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试按照此处提供的文档 http://symfony.com/doc/current/bundles/extension.html 在我的应用程序中导入 yaml 配置文件但我总是收到错误消息:
There is no extension able to load the configuration for "app"
我的文件位于此处:config/packages/app.yaml 并具有以下结构:
app:
list:
model1:
prop1: value1
prop2: value2
model2:
...
由于这是一个简单的应用程序,所有文件都位于src/
中。所以我有 src/DependencyInjection/AppExtension.php
<?php
namespace App\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
}
}
和src/DependencyInjection/Configuration.php
<?php
namespace App\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('app');
// Node definition
$rootNode
->children()
->arrayNode('list')
->useAttributeAsKey('name')
->requiresAtLeastOneElement()
->prototype('array')
->children()
->requiresAtLeastOneElement()
->prototype('scalar')
->end()
->end()
->end()
->end()
->end();
return $treeBuilder;
}
}
我无法访问我的参数:(
有什么想法吗?
最佳答案
如果您想使用扩展类加载自定义配置文件来处理其参数(就像在 Symfony bundle 扩展中一样,但不创建 bundle ),最终“创建”并添加一个或将更多内容添加到“容器”中(在编译之前),您可以在 Kernel.php< 中包含的
文件:configureContainer
方法中手动注册您的扩展类
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
// to avoid the same error you need to put this line at the top
// if your file is stored under "$this->getProjectDir().'/config'" directory
$container->registerExtension(new YourAppExtensionClass());
// ----- rest of the code
}
然后你就可以像往常一样使用你的参数 registering a Compiler Pass .
希望这有帮助。
关于symfony - 如何在 DI 扩展类中加载、处理和使用 Yaml 配置文件中的自定义参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47932045/
我是一名优秀的程序员,十分优秀!