【发布时间】:2017-08-03 14:00:24
【问题描述】:
编辑:删除 YamlFileLoader 行后,我处理的配置为空(数组(0){}):
我的新扩展
<?php
namespace MyProject\MyBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class MyProjectMyExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration,$configs);
var_dump($config); // array(0) { }
/*...*/
}
}
相关错误
ContextErrorException in MyProjectMyExtension.php line 22: Notice: Undefined index: cachedir
in MyProjectMyExtension.php line 22
at ErrorHandler->handleError('8', 'Undefined index: cachedir', '/home/jeff/Projets/MyProject/src/MyProject/MyBundle/DependencyInjection/MyProjectMyExtension.php', '22', array('configs' => array(array()), 'container' => object(ContainerBuilder), 'configuration' => object(Configuration), 'config' => array())) in MyProjectMyExtension.php line 22
at MyProjectMyExtension->load(array(array()), object(ContainerBuilder)) in MergeExtensionConfigurationPass.php line 59
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in MergeExtensionConfigurationPass.php line 39
at MergeExtensionConfigurationPass->process(object(ContainerBuilder)) in Compiler.php line 104
at Compiler->compile(object(ContainerBuilder)) in ContainerBuilder.php line 598
at ContainerBuilder->compile() in Kernel.php line 514
at Kernel->initializeContainer() in Kernel.php line 133
at Kernel->boot() in Kernel.php line 182
at Kernel->handle(object(Request)) in app_dev.php line 27
看来我的配置还是真的坏了。感谢您的评论;你知道这里出了什么问题吗?
我遇到了 Symfony 2.8 第三方捆绑配置问题。
我正在为我的包创建自定义配置,但 YamlFileLoader 似乎无法接受我在 config.yml 中写的内容:
InvalidArgumentException in YamlFileLoader.php line 399: There is no extension able to load the configuration for "mybundle_tools" (in /home/jeff/Projets/MyProject/src/MyProject/MyBundle/DependencyInjection/../Resources/config/config.yml). Looked for namespace "mybundle_tools", found none
我好几天都没有让这个自定义配置工作,我认为我的 Configuration.php 和我的 Extension.php 文件中也可能存在一些错误。
你能帮我找出我做错的地方吗?
感谢您的提前! :)
这是我的代码:
services.yml
services:
mybundle.weather:
class: MyProject\MyBundle\Service\Weather
arguments: ['%mybundle_tools%']
config.yml
mybundle_tools:
cachedir: '%kernel.cache_dir%/mybundle'
weather:
cachefile: '%kernel.cache_dir%/mybundle/weather.json'
apikey: 'myapikey'
apiurlbase: 'http://api.openweathermap.org/data/2.5/weather?q='
Configuration.php
<?php
namespace MyProject\MyBundle\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('mybundle_tools');
$rootNode
->children()
->scalarNode('cachedir')->end()
->arrayNode('weather')
->children()
->scalarNode('cachefile')->end()
->scalarNode('apikey')->end()
->scalarNode('apiurlbase')->end()
->end()
->end()
->end()
;
return $treeBuilder;
}
}
MyProjectMyExtension.php
<?php
namespace MyProject\MyBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class MyProjectMyExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
var_dump($configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('config.yml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration,$configs);
$container->setParameter('mybundle_tools.cachedir', $config['cachedir']);
$container->setParameter('mybundle_tools.weather.cachefile', $config['weather']['cachefile']);
$container->setParameter('mybundle_tools.weather.apikey', $config['weather']['apikey']);
$container->setParameter('mybundle_tools.weather.apiurlbase', $config['weather']['apiurlbase']);
}
}
【问题讨论】:
标签: php symfony dependency-injection symfony-2.8