2016-10-23 7 views
0

私はAppBundle内でいくつかのパラメータを成功裏に定義しようとしています。このフレームワークに関する私の最初のテストの1つで、私が誤解したという明白な概念があるかもしれません。Symfony 3.1 - AppBundleのグローバルパラメータを作成する

最初の問題は、フィールドbundle_versionで、YAMLファイル内には読み込まれません。私は、この行をコメントすると

[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 /依存性の注入/ 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'); 
    } 
} 

答えて

0

ソリューションは、事前に定義された「パラメータ」キーがあります/app/config/config.ymlで...非常に簡単であり、我々はしていますバンドル固有でない場合は、ここでパラメータをパターする必要があります。

だから、それだけのようなものです:あなたのコントローラで次に

# /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 

// /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, 
      ]); 
     } 
    } 

は、それが

を役に立てば幸い
関連する問題