2012-04-15 28 views
5

tôi đang theo dõi "Làm thế nào để lộ một cấu hình Semantic cho một Bundle" hướng dẫn chính thức cho symfony 2.cách truy cập cấu hình ngữ nghĩa trong bộ điều khiển với symfony2?

Tôi có configuration.php của tôi

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; 
    } 
} 

Và 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'); 
    } 
} 

Nghe có vẻ ngớ ngẩn, nhưng tôi không thể tìm ra cách, cách truy cập thông số logintext trong bộ điều khiển?

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

không hoạt động.

Tôi đang làm gì sai?

Trả lời

10

Trong w9UserExtension.php sau processConfiguration dòng chỉ cần thêm

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

Wow. Cảm ơn bạn. – dunqan

+3

Điều gì sẽ xảy ra nếu tôi có một loạt các tham số cấu hình, làm thế nào tôi có thể đặt chúng cùng một lúc? – acme

+0

@acme Tôi cũng muốn làm điều đó. Xem câu trả lời của tôi dưới đây. – LaGoutte

0

tôi muốn bừa bãi thêm tất cả giá trị cấu hình của tôi để các thông số, như @acme, và viết hàng chục setParameter dòng là không đủ lười biếng.

Vì vậy, tôi đã thực hiện phương thức setParameters để thêm vào lớp 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); 
    } 
} 

mà sau đó bạn có thể sử dụng như thế này:

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

     // ... 
    } 
} 

Cảnh báo

Đây có thể là BAD THỰC HÀNH nếu bạn không rộng rãi tài liệu và/hoặc cần một hành vi như vậy , vì bạn có thể hiển thị thông tin cấu hình nhạy cảm nếu bạn quên thông tin đó và mất hiệu suất bằng cách hiển thị cấu hình vô dụng.

Bên cạnh đó, nó ngăn cản bạn khử trùng rộng rãi các biến cấu hình trước khi thêm chúng vào túi tham số của vùng chứa.

Nếu bạn đang sử dụng điều này, có thể bạn nên sử dụng parameters: chứ không phải cấu hình ngữ nghĩa, trừ khi bạn biết mình đang làm gì.

Tự chịu rủi ro.

Các vấn đề liên quan