编辑:删除YamlFileLoader行后,我处理的配置为空(array(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']);
}
}
答案 0 :(得分:0)
您已经使用load
方法配置了捆绑包。如果您使用processConfiguration
,那么您将收到捆绑包。
请记住,load方法仅包含您的捆绑配置。如果要访问完整配置,则需要实现prepend
接口。
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
....
}