【发布时间】:2016-04-26 18:32:04
【问题描述】:
我想在我的 AppBundle 中添加一个配置条目(使用 Symfony 3.0.3)。
我坚持的是:
尽管我努力尝试坚持文档示例并寻找有关 SO 的修复程序,但我仍然收到以下异常并且无法发现问题所在。
YamlFileLoader.php 第 368 行中的 InvalidArgumentException:
没有扩展能够加载“app”的配置 (在 /.../src/AppBundle/DependencyInjection /../Resources/config/config.yml)。寻找命名空间“app”,没有找到
存在线程/文章,但没有建议的解决方案实际上为我修复了它。有些细节可能一目了然,我希望有人能在我花费数小时的时候立即发现这些。
到目前为止我做了什么:
我首先设置了这个 conf 文件
我猜“app”键应该没问题,因为doc 期望名称是小写的包名称,其中 bundle 部分被剥离。对吧?
# AppBundle/Resources/config/config.yml
app:
paginator:
items_per_page: 3
然后我在 DependencyInjection 命名空间内设置加载/设置过程所需的 php 类...
配置类文件:
# /src/AppBundle/DependencyInjection/Configuration.php
namespace AppBundle\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');
$rootNode
->children()
->arrayNode('paginator')
->performNoDeepMerging()
->addDefaultsIfNotSet()
->children()
->integerNode('items_per_page')
->defaultValue(5)
->isRequired()
->cannotBeEmpty()
->end()
->end()
->end()
->end();
return $treeBuilder;
}
}
扩展类文件:
相应地命名为doc。
# /src/AppBundle/DependencyInjection/AppExtension.php
namespace AppBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('config.yml');
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
}
}
堆栈跟踪
in YamlFileLoader.php line 368
at YamlFileLoader->validate(array('app' => array('paginator' => array('items_per_page' => '3'), 'mailer' => array('sender_name' => 'no-reply', 'sender_address' => '%mailer_user%'))), '/../project/src/AppBundle/DependencyInjection/../Resources/config/config.yml') in YamlFileLoader.php line 338
at YamlFileLoader->loadFile('/../project/src/AppBundle/DependencyInjection/../Resources/config/config.yml') in YamlFileLoader.php line 44
at YamlFileLoader->load('config.yml') in AppExtension.php line 21
at AppExtension->load(array(array()), object(ContainerBuilder)) in MergeExtensionConfigurationPass.php line 55
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in MergeExtensionConfigurationPass.php line 39
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in Compiler.php line 107
at Compiler->compile(object(ContainerBuilder)) in ContainerBuilder.php line 545
at ContainerBuilder->compile() in Kernel.php line 477
at Kernel->initializeContainer() in Kernel.php line 117
at Kernel->boot() in Kernel.php line 166
at Kernel->handle(object(Request)) in app_dev.php line 30
谢谢。
【问题讨论】:
标签: symfony dependency-injection