###使用Bundle擴展類php
在app/config/config.yml中爲本身bundle配置參數 acme_demo:#這個爲須要配置bundle的別名 demo: client_id: 123 client_secret: xxxx
在上一篇如何加載服務中講到如何建立一個擴展類;在擴展類中處理這些配置參數數組
在load()方法中symfony自動會把YML或XML格式的配置參數轉換成數組了app
array( // values from config.yml array( 'demo' => array( 'client_id' => 123, 'client_secret' => xxx, ), ), // values from config_dev.yml array( 'demo' => array( 'client_id' => 456, ), ), )
須要注意這是一個多維數組;各類環境下加載的配置不同;ui
symfony提供了方法可以讓根據環境不一樣選擇不一樣的參數並且還能夠驗證參數的正確性this
####添加配置類spa
// src/Acme/DemoBundle/DependencyInjection/Configuration.php namespace Acme\DemoBundle\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('acme_social'); $rootNode ->children() ->arrayNode('demo') ->children() ->integerNode('client_id')->end()#整形節點 ->scalarNode('client_secret')->end()#標量類型 ->end() ->end() // twitter ->end() ; return $treeBuilder; } }
在load()方法中驗證以及合併配置.net
public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); // ... }
能夠讓擴展類繼承ConfigurableExtension來減小每次都要調用processConfiguration方法來驗證合併配置scala
class Test2Extension extends ConfigurableExtension { //注意這個方法名稱不是load; protected function loadInternal(array $mergedConfig, ContainerBuilder $container) { // ... } }