2012-12-06 118 views
18

創建我自己的FacebookBundle和沒有擴展能夠加載配置「facebookbundle」 Symfony2的

我得到這個錯誤:

There is no extension able to load the configuration for "facebookbundle" (in /facebookx/app/config/config_dev.yml). Looked for namespace "facebookbundle", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "jms_aop", "jms_di_extra", "jms_security_extra", "d_facebook", "d_user", "d_security", "web_profiler", "sensio_distribution"

該錯誤消息意味着我得到了一個條目「facebookbundle 「在我的config.yml中沒有被任何擴展名使用?

我config.yml

facebookbundle: 
    file: %kernel.root_dir%/../src/FacebookBundle/Facebook/FacebookInit.php 
    alias: facebook 
    app_id: xxx 
    secret: xxx 
    cookie: true 
    permissions: [email, user_birthday, user_location, user_about_me] 

我DFacebookExtension

<?php 

namespace D\FacebookBundle\DependencyInjection; 

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

/** 
* This is the class that loads and manages your bundle configuration 
* 
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} 
*/ 
class DFacebookExtension 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'); 

     foreach (array('app_id', 'secret', 'cookie', 'permissions') as $attribute) { 
      $container->setParameter('facebookbundle.'.$attribute, $config[$attribute]); 
     } 

     if (isset($config['file']) && $container->hasDefinition('acebookbundle.api')) { 
      $facebookApi = $container->getDefinition('facebookbundle.api'); 
      $facebookApi->setFile($config['file']); 
     } 
    } 
} 

均是錯誤?

回答

17

爲了自定義配置參數被接受,你必須定義使用內的configuration.php類的束配置你的包。

的src/FacebookBundle/DependencyInjection /的configuration.php:

<?php 
namespace FacebookBundle\DependencyInjection; 

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

/** 
* This is the class that validates and merges configuration from your app/config files 
* 
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} 
*/ 
class Configuration implements ConfigurationInterface 
{ 
    public function getConfigTreeBuilder() 
    { 
     $treeBuilder = new TreeBuilder(); 
     $rootNode = $treeBuilder->root('facebookbundle'); 

     $rootNode 
      ->children() 
       ->scalarNode('file')->defaultValue('')->end() 
       ->scalarNode('alias')->defaultValue('')->end() 
       ->scalarNode('app_id')->defaultValue('')->end() 
       ->scalarNode('secret')->defaultValue('')->end() 
       ->booleanNode('cookie')->defaultTrue()->end() 
       ->arrayNode('permissions') 
        ->canBeUnset()->prototype('scalar')->end()->end() 
      ->end() 
      ; 

     return $treeBuilder; 
    } 
} 
?> 
+0

okej,謝謝。你現在我怎麼能在我的課堂上獲得這些數據(例如app_id,祕密)? –

+0

我編輯了我的答案,以包含基於您原始YML配置格式的基本配置定義。我沒有測試過,所以可能需要稍微調整一下。 – lifo

+0

我的問題可能會揭示一些 - http:// stackoverflow。com/questions/32482622/creating-a-configuration-file-in-symfony2/32502618#32502618 – Peter

6

這種情況發生時,你忘了啓動應用程序捆綁/ AppKernel.php:

<?php 

use Symfony\Component\HttpKernel\Kernel; 
use Symfony\Component\Config\Loader\LoaderInterface; 

class AppKernel extends Kernel 
{ 

    public function registerBundles() 
    { 
     $bundles = array (
       new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), 
       new Symfony\Bundle\SecurityBundle\SecurityBundle(), 
       new Symfony\Bundle\TwigBundle\TwigBundle(), 
       new Symfony\Bundle\MonologBundle\MonologBundle(), 
       new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(), 
       new Symfony\Bundle\DoctrineBundle\DoctrineBundle(), 
       new Symfony\Bundle\AsseticBundle\AsseticBundle(), 
       new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), 
       new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(), 
       //... 
       new D\FacebookBundle\DFacebookBundle(), 
       //... 
    ); 

     if (in_array($this->getEnvironment(), array ('dev', 'test'))) 
     { 
     $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); 
     $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); 
     $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); 
     } 

     return $bundles; 
    } 

    public function registerContainerConfiguration(LoaderInterface $loader) 
    { 
     $loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml'); 
    } 

} 
+0

新的FOS \ FacebookBundle \ FOSFacebookBundle()?我寫我自己的FacebookBundle –

+0

oups,當然你自己的捆綁!我在你的src/FacebookBundle/DependencyInjection/Configuration.php中看到一個潛在的錯誤,命名空間錯過了前綴D \ – AlterPHP

+0

錯誤在於lifo的答案,不確定它是否相關。 – AlterPHP

42

另外,請記住,配置文件的根密鑰必須是包的名字的標準化形式。

這是我曾經經歷過的幾次,如果你不知道這一點,解決起來會非常沮喪。

示例:如果捆綁包名爲MyFirstAwesomeBundle,則文件中的根密鑰必須爲my_first_awesome。所以駱駝案件被轉化爲蛇案,而「捆綁」一詞被忽略或剝離。

因此,僅僅讓文件中的根密鑰與Configuration::getConfigTreeBuilder()中指定的值完全匹配是不夠的。

如果你不遵循這個規則,那麼你會得到There is no extension able to load the configuration錯誤。


我希望這將有助於在未來絕望的靈魂誰這個網頁上結束了......

+1

是的,這的確幫助了我! :) – Tarun

+0

YourBundleExtension是否存在於您的捆綁軟件依賴目錄中?在某些情況下,這個原因使**沒有擴展能夠加載配置**錯誤發生。 –

+0

Omg。你是金。因爲這個,我的腦袋被撞出來了。 –

1

此錯誤也可以通過一個缺少services關鍵在你的配置的根本原因造成的:

services: 
    foo_bar.baz: 
     class: Foo\BarBundle\Service\BazService 
相關問題