2012-04-15 12 views
5

Obserwuję "Jak wystawiać semantycznego konfiguracja dla Bundle" oficjalnej instrukcji dla Symfony 2.jak uzyskać dostęp do konfiguracji semantycznej w kontrolerze z symfony2?

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

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

Może to zabrzmieć głupio, ale nie jestem w stanie znaleźć sposobu, w jaki sposób uzyskać dostęp do parametru logintext w kontrolerze?

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

nie działa.

Co robię źle?

Odpowiedz

10

W w9UserExtension.php po processConfiguration linia wystarczy dodać

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

Wow. Dziękuję Ci. – dunqan

+3

Co zrobić, jeśli mam kilka parametrów konfiguracyjnych, w jaki sposób ustawić je wszystkie naraz? – acme

+0

@acme Chciałem to również zrobić. Zobacz moją odpowiedź poniżej. – LaGoutte

0

Chciałem bezkrytycznie dodać wszystkie moje wartości konfiguracyjne z parametrami, jak @acme i pisania dziesiątki setParameter linii nie był na tyle leniwy.

Tak więc stworzyłem metodę setParameters do dodania do klasy 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); 
    } 
} 

, które można następnie wykorzystać tak:

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

     // ... 
    } 
} 

Ostrzeżenie

To może być złą praktyką jeśli nie obszernie dokumentuje i/lub potrzebują takiego zachowania , ponieważ możesz ujawnić poufne informacje konfiguracyjne, jeśli zapomnisz o tym i stracisz wydajność, ujawniając niepotrzebną konfigurację.

Poza tym, zapobiega to intensywnemu czyszczeniu zmiennych konfiguracyjnych przed dodaniem ich do worka parametrów kontenera.

Jeśli używasz tego, prawdopodobnie powinieneś używać parameters:, a nie konfiguracji semantycznej, chyba że wiesz, co robisz.

Używaj na własne ryzyko.

Powiązane problemy