doushou1298 2016-10-23 13:01
浏览 57
已采纳

Symfony 3.1 - 为AppBundle创建全局参数

I'm trying to define some parameters inside the AppBundle without success. It's one of my first test on this framework so there may be obvious concept that I misunderstood.

The first problem is the field bundle_version which is not read inside the YAML file. The error returned is the following :

[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The child node "bundle_version" at path "costs" must be configured.

When i comment this line, i get another error :

[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');
    }
}
  • 写回答

1条回答 默认 最新

  • duankaolei2921 2016-10-28 17:39
    关注

    The solution is quite simple as i expected ... In /app/config/config.yml there's a predefined "parameters" key and we're supposed to putt our params here if they're not Bundle specific.

    So it's just something like :

    # /app/config/config.yml
    imports:
        - { resource: parameters.yml }
        - { resource: security.yml }
        - { resource: services.yml }
    
    # Put parameters here that don't need to change on each machine where the app is deployed
     # http://symfony.com/doc/current/best_practices/configuration.html#application-    related-configuration
    parameters:
          locale: fr
    my_param: my_value
    

    Then in your controller :

    //   /src/AppBundle/Controller/DefaultController.php
    
        class DefaultController extends Controller
        {
            /**
             * @Route("/", name="homepage")
             */
            public function indexAction(Request $request)
            {
                $this->getParameter('my_param'); // or $this->getContainer()->getParameter('my_param');
    
                // replace this example code with whatever you need
                return $this->render('default/index.html.twig', [
                    'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
                ]);
            }
        }
    

    Hope it helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突