34

我创建了自己的 FacebookBundle 和

我收到了这个错误:

没有扩展能够加载“facebookbundle”的配置(在 /facebookx/app/config/config_dev.yml 中)。寻找命名空间“facebookbundle”,找到“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”

错误消息意味着我在我的 config.yml 中有一个条目“facebookbundle”,它没有被任何扩展使用?

我的 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]

我的 DFacebook 分机

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

是错误吗?

4

4 回答 4

73

另外,请记住,配置文件的根键必须是 Bundle 名称的规范化形式

这是我遇到过几次的事情,如果你不知道的话,解决它会非常令人沮丧。

示例:如果调用了 bundle MyFirstAwesomeBundle,则文件中的根键必须是my_first_awesome. 所以camel-case 被转换成snake-case 并且“bundle”这个词被忽略或者去掉了。

因此,仅仅让文件中的根键与指定的值完全匹配Configuration::getConfigTreeBuilder()是不够的。

如果您不遵循此规则,那么您将收到There is no extension able to load the configuration错误消息。


我希望这将帮助下一个最终出现在此页面上的绝望灵魂......

于 2016-02-19T12:18:07.060 回答
25

为了接受自定义配置参数,您必须使用捆绑包中的 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;
    }
}
?>
于 2012-12-06T12:11:50.640 回答
13

当您忘记在 app/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');
   }

}
于 2012-12-06T10:58:53.157 回答
6

此错误也可能是由于services配置根目录中缺少或无效的键引起的。它应该看起来像这样:

services:
    foo_bar.baz:
        class: Foo\BarBundle\Service\BazService
于 2017-05-10T10:38:33.263 回答