2016-10-23 13:01
浏览 56

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 :

The child node "bundle_version" at path "costs" must be configured.

When i comment this line, i get another error :

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


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');

                    // THIS LINE DON'T WORK AND CAUSE THE 1st PROBLEM


        return $treeBuilder;


    bundle_version: 1.0 # THIS FIELD IS NOT RECOGNIZED
        es: 18.00
        it: 19.00
        uk: 20.00
        serviceA: 15.00
        serviceB: 20.00


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(
                new FileLocator(__DIR__.'/../Resources/config')
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
        - { 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
     #    related-configuration
          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

    打赏 评论

相关推荐 更多相似问题