Symfony自定义配置 - 命名空间问题

时间:2015-08-02 08:43:03

标签: php symfony

根据Loading custom config file for app in symfony2发布的建议,我陷入了命名空间问题。

\src\AppBundle\DependencyInjection我有两个文件:

AppExtension.php:

<?php
namespace AppBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use AppBundle\DependencyInjection\Configuration;

/**
 * This is the class that loads and manages your bundle configuration
 */
class AppExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('kh', $config['kh']);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('general.yml'); # another file of yours

    }
}

Configuration.php

<?
namespace AppBundle\DependencyInjection;

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

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

         $rootNode
            ->children()
            ->arrayNode('tags')
            ->prototype('array')
                ->children()
                    ->scalarNode('name')->isRequired()->end();
                    ->scalarNode('role')->isRequired()->end();
                    ->scalarNode('priority')->isRequired()->end();
                    ->scalarNode('label')->isRequired()->end();
                ->end();
            ->end();
        ->end();

        return $treeBuilder;
    }
}

似乎我犯了某种错误,将文件命名为执行:

php app/console cache:clear --env=prod

结果:

  

PHP致命错误:第22行/var/www/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/AppExtension.php中找不到类'AppBundle \ DependencyInjection \ Configuration'       [2015-08-02 10:39:36] php.EMERGENCY:致命错误:未找到类'AppBundle \ DependencyInjection \ Configuration'{“type”:1,“file”:“/ var / www / dev.investmentopportunities。 PL / SRC /的appbundle / DependencyInjection / AppExtension.php”, “线”:22, “电平”:6143, “堆叠”:[]}

不幸的是我遇到了自己发现错误的问题。我试图重命名文件但仍然发生错误。你能告诉我我做错了什么吗?

更新1:

由于@San Thapa的消息,我也尝试删除“使用”命令。这导致:

  

PHP致命错误:第21行/var/www/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/AppExtension.php中找不到类'AppBundle \ DependencyInjection \ Configuration'       [2015-08-02 12:05:27] php.EMERGENCY:致命错误:未找到类'AppBundle \ DependencyInjection \ Configuration'{“type”:1,“file”:“/ var / www / dev.investmentopportunities。 PL / SRC /的appbundle / DependencyInjection / AppExtension.php”, “线”:21, “电平”:6143, “堆叠”:[]}

     

[Symfony的\元器件\调试\异常\的ClassNotFoundException]     尝试从命名空间“AppBundle \ Dependenc”加载类“Configuration”     yInjection”。     您是否忘记了“使用”声明,例如“的Symfony \包\ TwigBundle \依靠     encyInjection \ Configuration“,”Symfony \ Bundle \ DebugBundle \ DependencyInjecti     on \ Configuration“,”Symfony \ Bundle \ WebProfilerBundle \ DependencyInjection \ Co     配置“,”Symfony \ Bundle \ FrameworkBundle \ Tests \ Functional \ Bundle \ TestB     undle \ DependencyInjection \ Configuration“,”Symfony \ Bundle \ FrameworkBundle \ D.     ependencyInjection \ Configuration“,”Genemu \ Bundle \ FormBundle \ DependencyInje     ction \ Configuration“,”Doctrine \ ORM \ Configuration“,”Doctrine \ DBAL \ Configur     ation“,”Doctrine \ Bundle \ DoctrineCacheBundle \ DependencyInjection \ Configurat     ion“,”Symfony \ Bundle \ SwiftmailerBundle \ DependencyInjection \ Configuration“,      “Symfony \ Bundle \ MonologBu​​ndle \ DependencyInjection \ Configuration”,“Symfony     \ Bundle \ AsseticBundle \ DependencyInjection \ Configuration“,”Sensio \ Bundle \ Fr.     ameworkExtraBundle \ DependencyInjection \ Configuration“,”PUGX \ AutocompleterB     undle \ DependencyInjection \ Configuration“,”Knp \ Bundle \ PaginatorBundle \ Depen     dencyInjection \ Configuration“,”FOS \ UserBundle \ DependencyInjection \ Configur     “或”Doctrine \ Bundle \ DoctrineBundle \ DependencyInjection \ Configuration     “?

从浏览器访问网页看起来有点奇怪并不会导致任何错误。

1 个答案:

答案 0 :(得分:1)

刚刚添加到Configuration.php的第一行:

<?php

并删除分号:

$rootNode
        ->children()
        ->arrayNode('tags')
        ->prototype('array')
            ->children()
                ->scalarNode('name')->isRequired()->end()
                ->scalarNode('role')->isRequired()->end()
                ->scalarNode('priority')->isRequired()->end()
                ->scalarNode('label')->isRequired()->end()
            ->end()
        ->end()
    ->end();

我对结构不确定......也许你忘了添加一个结尾()......