没有扩展程序可以加载“my_bundle_name”的配置

时间:2013-09-14 14:49:12

标签: php symfony dependency-injection bundle

我知道这篇文章在这里很流行,关于这个问题有很多问题,但没有什么能帮助我解决我的问题。我不得不问这个。

我创建了一个名为“ATL15 / GoogleAnalyticsBundle”的软件包。

我想从app / config.yml获取用户参数;这是我的配置参数,我正在从app / parameters.yml加载参数。

atl15_google_analytics:
    client_id:  "%ga_client_id%"
    client_secret: "%ga_client_secret%"
    developer_key: "%ga_developer_key%"
    redirect_uri: "%ga_redirect_uri%"

我做了我从symfony文档书和网页上读到的所有内容。什么都没有帮助我...

这是我的DependencyInjection/Configuration.php文件:

<?php

namespace ATL15\GoogleAnalyticsBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('atl15_google_analytics');

        $rootNode->children()
                    ->scalarNode('client_id')->isRequired()->cannotBeEmpty()->end()
                    ->scalarNode('client_secret')->isRequired()->cannotBeEmpty()->end()
                    ->scalarNode('developer_key')->isRequired()->cannotBeEmpty()->end()
                    ->scalarNode('redirect_uri')->isRequired()->cannotBeEmpty()->end()
                 ->end();

        //var_dump($rootNode); die;

        return $treeBuilder;
    }
}

这是我的DependencyInjection/ATL15GoogleAnalyticsBundleExtension.php文件:

<?php

namespace ATL15\GoogleAnalyticsBundle\DependencyInjection;

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

class ATL15GoogleAnalyticsExtension 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'));

        foreach (array('config') as $basename) {
            $loader->load(sprintf('%s.yml', $basename));
        }

        foreach (array('client_id', 'client_secret', 'developer_key', 'redirect_uri') as $attribute) {
            $container->setParameter($attribute, $config[$attribute]);
        }
    }

    public function getAlias()
    {
        return 'atl15_google_analytics';
    }
}

是的,我从app/AppKernel.php;

加载了这个包
    new ATL15\GoogleAnalyticsBundle\ATL15GoogleAnalyticsBundle(),

每次我收到此错误:

  

[Sat Sep 14 17:37:24 2013] [错误] [client 127.0.0.1] PHP致命错误:   未捕获的异常   '的Symfony \元器件\ DependencyInjection \异常\ InvalidArgumentException'   消息'没有可以加载配置的扩展名   “atl15_google_analytics”(in   /var/www/vsy-bio/src/ATL15/GoogleAnalyticsBundle/DependencyInjection/../Resources/config/config.yml)。   查找名称空间“atl15_google_analytics”,找不到“   /var/www/vsy-bio/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php:290\nStack   追踪:\ n#0   /var/www/vsy-bio/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php(260):   的Symfony \组件\ DependencyInjection \装载机\ YamlFileLoader-&GT;验证(阵列,   “/var/www/vsy-bi...')\n#1   /var/www/vsy-bio/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php(44):   的Symfony \组件\ DependencyInjection \装载机\ YamlFileLoader-&GT;的loadFile( '/无功/网络/ VSY-BI ...')\ N#2   /var/www/vsy-bio/src/ATL15/GoogleAnalyticsBundle/DependencyInjection/ATL15GoogleAnalyticsExtension.php(28):   Symfon in   /var/www/vsy-bio/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php   在第290行

你能帮我吗?

3 个答案:

答案 0 :(得分:1)

ATL15GoogleAnalyticsExtension的外观和您的错误看起来,您正在从您的捆绑包Resources \ config加载一个名为config.yml的文件,该文件正在使用app\config\config.yml中所述的参数。

文件ATL15/GoogleAnalyticsBundle/DependencyInjection/../Resources/config/config.yml应该只包含2个名称空间parametersservices ...

parameters:
    atl15_google_analytics.something.class: ATL15/GoogleAnalyticsBundle...

services:
    atl15_google_analytics.something.services:
        class: %atl15_google_analytics.something.class%

配置数据由$config = $this->processConfiguration($configuration, $configs);传递到扩展文件,而不是您需要调用文件本身,因此您不需要加载Resources/config/config.yml,除非实际包含任何内部服务或捆绑包的参数。

答案 1 :(得分:0)

有一个多余的foreach:

foreach (array('config') as $basename) {
    $loader->load(sprintf('%s.yml', $basename));
}

除非您的bundle的Resources文件夹中有config.yml文件,并且您知道自己在做什么,否则请删除此foreach。您提供的堆栈跟踪中的行号与源代码不匹配,因此我猜您之后编辑了它,但我认为错误来自此config.yml文件。

您不必调用$ loader-&gt; load(),为了从app / config.yml读取参数,它会自动完成。

答案 2 :(得分:-1)

错误是Extension类文件的名称,也是匹配bundle所需的类名:

应该是GoogleAnalyticsExtension而不是ATL15GoogleAnalyticsBundleExtension,文件名必须是GoogleAnalyticsExtension.php,并将atl15_google_analytics更改为google_analytics