2012-04-15 117 views
5

我下面的 「如何公開語義配置的軟件包」 官方手冊爲symfony1.2 2.如何使用symfony2訪問控制器中的語義配置?

我有我的configuration.php

namespace w9\UserBundle\DependencyInjection; 

use Symfony\Component\Config\Definition\Builder\TreeBuilder; 
use Symfony\Component\Config\Definition\ConfigurationInterface; 

class Configuration implements ConfigurationInterface 
{ 
    /** 
    * {@inheritDoc} 
    */ 
    public function getConfigTreeBuilder() 
    { 
     $treeBuilder = new TreeBuilder(); 
     $rootNode = $treeBuilder->root('w9_user'); 

     $rootNode 
      ->children() 
       ->scalarNode('logintext')->defaultValue('Zareejstruj się')->end() 
      ->end() 
     ;   

     return $treeBuilder; 
    } 
} 

而且w9UserExtension.php:

namespace w9\UserBundle\DependencyInjection; 

use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\Config\FileLocator; 
use Symfony\Component\HttpKernel\DependencyInjection\Extension; 
use Symfony\Component\DependencyInjection\Loader; 

class w9UserExtension extends Extension 
{ 
    /** 
    * {@inheritDoc} 
    */ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 

     $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 
     $loader->load('services.yml'); 
    } 
} 

這可能聽起來很愚蠢,但我無法找到方法,如何訪問控制器中的logintext參數?

$logintext = $this->container->getParameter("w9_user.logintext"); 

不起作用。

我在做什麼錯了?

回答

10

w9UserExtension.phpprocessConfiguration線只需添加

$container->setParameter('w9_user.logintext', $config['logintext']); 
+1

哇。謝謝。 – dunqan 2012-04-15 21:16:10

+3

如果我有一堆配置參數會怎麼樣,我該如何設置它們全部一次? – acme 2013-09-09 15:11:19

+0

@acme我也想這樣做。請參閱下面的答案。 – LaGoutte 2016-06-01 05:50:20

0

我想亂加所有我的配置值的參數,如@acme,寫幾十個setParameter線是不夠的懶惰。

所以,我做了一個setParameters的方法添加到Extension類。

/** 
* Set all leaf values of the $config array as parameters in the $container. 
* 
* For example, a config such as this for the alias w9_user : 
* 
* w9_user: 
* logintext: "hello" 
* cache: 
*  enabled: true 
* things: 
*  - first 
*  - second 
* 
* would yield the following : 
* 
* getParameter('w9_user.logintext') == "hello" 
* getParameter('w9_user.cache') ---> InvalidArgumentException 
* getParameter('w9_user.cache.enabled') == true 
* getParameter('w9_user.things') == array('first', 'second') 
* 
* It will resolve `%` variables like it normally would. 
* This is simply a convenience method to add the whole array. 
* 
* @param array $config 
* @param ContainerBuilder $container 
* @param string $namespace The parameter prefix, the alias by default. 
*       Don't use this, it's for recursion. 
*/ 
protected function setParameters(array $config, ContainerBuilder $container, 
           $namespace = null) 
{ 
    $namespace = (null === $namespace) ? $this->getAlias() : $namespace; 

    // Is the config array associative or empty ? 
    if (array_keys($config) !== range(0, count($config) - 1)) { 
     foreach ($config as $k => $v) { 
      $current = $namespace . '.' . $k; 
      if (is_array($v)) { 
       // Another array, let's use recursion 
       $this->setParameters($v, $container, $current); 
      } else { 
       // It's a leaf, let's add it. 
       $container->setParameter($current, $v); 
      } 
     } 
    } else { 
     // It is a sequential array, let's consider it as a leaf. 
     $container->setParameter($namespace, $config); 
    } 
} 

,你就可以使用這樣的:

class w9UserExtension extends Extension 
{ 
    /** 
    * {@inheritDoc} 
    */ 
    public function load(array $configs, ContainerBuilder $container) 
    { 
     $configuration = new Configuration(); 
     $config = $this->processConfiguration($configuration, $configs); 

     // Add them ALL as container parameters 
     $this->setParameters($config, $container); 

     // ... 
    } 
} 

警告

這可能是不好的做法如果沒有廣泛的文檔和/或需要這樣的行爲,因爲如果您忘記了敏感配置信息,就可能暴露敏感配置信息,並通過暴露無用的配置而失去性能。

此外,它可以防止您在將配置變量添加到容器的參數包中之前廣泛地清理配置變量。

如果你使用這個,你應該使用parameters:而不是語義配置,除非你知道你在做什麼。

使用需要您自擔風險。

相關問題