2012-12-06 120 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 
相关问题