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

Posted

技术标签:

【中文标题】Symfony 自定义配置 - 命名空间问题【英文标题】:Symfony custom configuration - namespace issue 【发布时间】:2015-10-24 13:04:37 【问题描述】:

按照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","line":22,"level":6143,"stack":[]

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

更新 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","line":21,"level":6143,"stack":[]

[Symfony\Component\Debug\Exception\ClassNotFoundException] 试图从命名空间“AppBundle\Dependenc”加载类“配置” y 注射”。 您是否忘记了“使用”声明,例如“Symfony\Bundle\TwigBundle\Depend encyInjection\Configuration", "Symfony\Bundle\DebugBundle\DependencyInjecti on\Configuration", "Symfony\Bundle\WebProfilerBundle\DependencyInjection\Co nfiguration", "Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestB undle\DependencyInjection\Configuration", "Symfony\Bundle\FrameworkBundle\D pendencyInjection\Configuration", "Genemu\Bundle\FormBundle\DependencyInje 操作\配置”、“教义\ORM\配置”、“教义\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 ation”或“Doctrine\Bundle\DoctrineBundle\DependencyInjection\Configuration “?

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

【问题讨论】:

在这种情况下不需要使用命名空间,因为两个文件都驻留在同一个目录中,并且被同一个命名空间所包围,您可以直接使用它而无需use声明。 您好,感谢您提供线索。我一开始就试过了,但还是报错了。 如果您在开发环境中运行项目,请尝试在开发环境中清除缓存。大多数时候清除缓存都对我有用。 【参考方案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();

我不确定结构...也许您忘记再添加一个 end()...

【讨论】:

以上是关于Symfony 自定义配置 - 命名空间问题的主要内容,如果未能解决你的问题,请参考以下文章

自定义作曲家命名空间找不到类

Symfony 错误:在链配置的命名空间 App\Entity 中找不到类 xxx [重复]

Symfony2 - 在链配置的命名空间中找不到类“X”

在链配置的命名空间 App\Entity [Symfony 5.3][PHPUnit 8.5] 中找不到类 Mock_*

使用 Symfony 1.4 的自动加载器加载命名空间类?

[Yii2.0] 以Yii 2.0风格加载自定义类或命名空间 [配置使用Yii2 autoloader]