2012-07-25 78 views
3

我有一個名爲Web \ CoworkerBundle的軟件包。在DIC /的configuration.php我:Symfony 2配置參數 - 參數必須定義

$rootNode = $treeBuilder->root('web_coworker'); 
$rootNode 
    ->children() 
     ->scalarNode('redirect_url')->defaultNull()->end() 
    ->end(); 

在config.yml我:

web_coworker: 
    redirect_url: "http://www.example.com/" 
我DefaultController.php

現在,我做

return array(
    'url' => $this->container->getParameter('redirect_url') 
); 

我得到的錯誤

必須定義參數「redirect_url」。

我想念什麼嗎?

+0

你可以在' - > scalarstackoNde('redirect_url') - > defaultNull() - > end()'中輸入錯誤嗎? – 2012-07-25 12:24:02

+0

對不起,這是一個複製粘貼錯誤:)修正了它。 – GergelyPolonkai 2012-07-25 12:38:23

回答

14

您需要創建一個組合中的一個擴展(的Acme/DemoBundle/DependencyInjection

class AcmeDemoBundleExtension extends Extension 
{ 
    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'); 
     // MOST IMPORTANT LINE 
     $container->setParameter('web_coworker.params', $config); 
    } 
} 

現在就可以刪除你的回來時,這應該做的伎倆控制器:)

+4

「食譜」中的示例中沒有「最重要的行」!您可能還需要將數組的第一級變平,並查看FOS User Bundle如何執行此操作。 – 2013-08-03 23:20:02

相關問題