我试图在 AppBundle 中定义一些参数但没有成功。这是我对这个框架的第一次测试,所以可能有明显的概念我误解了。
第一个问题是在 YAML 文件中未读取的字段bundle_version 。返回的错误如下:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The child node "bundle_version" at path "costs" must be configured.
当我评论这一行时,我得到另一个错误:
[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for "costs" (in W:\wamp64\www\test\src\AppBundle\DependencyInjecti
on/../Resources/config\costs.yml). Looked for namespace "costs", found none
src/AppBundle/DependencyInjection/Configuration.php
namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('costs');
$rootNode->children()
// THIS LINE DON'T WORK AND CAUSE THE 1st PROBLEM
->floatNode('bundle_version')->isRequired()->end()
->arrayNode('shipping')
->children()
->floatNode('es')->isRequired()->end()
->floatNode('it')->isRequired()->end()
->floatNode('uk')->isRequired()->end()
->end()
->end()
->arrayNode('services')
->children()
->floatNode('serviceA')->isRequired()->end()
->floatNode('serviceB')->isRequired()->end()
->end()
->end()
->end();
return $treeBuilder;
}
}
/src/AppBundle/Resources/config.costs.yml
costs:
bundle_version: 1.0 # THIS FIELD IS NOT RECOGNIZED
shipping:
es: 18.00
it: 19.00
uk: 20.00
services:
serviceA: 15.00
serviceB: 20.00
/src/AppBundle/DependencyInjection/AppExtension.php
namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class AppExtension extends Extension
{
public function load(array $config, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('costs.yml');
}
}