0

我收到以下错误:

没有扩展能够加载“upload_images”的配置(在“/var/www/vhosts/diabetigraph-dev/vendor/verzeilberg/upload-images/src/Resources/services.yaml”)。寻找命名空间“upload_images”,找到“无”

这是我的文件:

服务.yaml

services:
  verzeilberg\UploadImagesBundle\Service\Rotate:
    autowire: true

upload_images:
  version: 100

配置

namespace verzeilberg\UploadImagesBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('upload_images');
        $treeBuilder->getRootNode()
                ->children()
                    ->integerNode('version')->end()
                ->end();
        return $treeBuilder;
    }
}

上传图片扩展

namespace verzeilberg\UploadImagesBundle\DependencyInjection;

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

class UploadImagesExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources'));
        $loader->load('services.yaml');
        $config = $this->processConfiguration(new Configuration(), $configs);
        $container->setParameter('version', $config['version']);
    }
}

我究竟做错了什么?

4

1 回答 1

0

基本答案是您的 upload_images 配置需要移动到它自己的应用程序级配置/包文件:

# app/config/packages/upload_images.yaml
upload_images:
  version: 100

在您的包中,配置对象代表包的配置。您的包中没有 upload_images.yaml 类型的文件。该对象非常强大,因此您可以添加默认值和选项等等。

您的包的扩展负责处理最终配置并使诸如参数之类的信息可用于系统的其余部分:

class UploadImagesExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        // $configs is actually an array of array representing the contents of upload_files.yaml
        $config = $this->processConfiguration(new Configuration(), $configs);
        $container->setParameter('upload_files.version', $config['version']);

        // Loading your bundle's services.yaml file is a different process which just happens to be kicked off by the loader
        // Thanks to the setParameter above, %upload_files.version% can be used by services
        $loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__).'/Resources'));
        $loader->load('services.yaml');
    }
}

这可能会令人困惑,至少对我而言,我必须多次阅读文档并进行大量实验以了解整个过程。

混淆是 Symfony 从每个应用程序的多个包发展到一个应用程序包,再到根本没有应用程序包的原因之一。

我还可以添加 Symfony 使用 composer recipes 来简化安装包。该配方不仅将捆绑包添加到 config/bundles.php,而且还将任何默认配置文件复制到 config/packages。不幸的是,这个副本需要额外的步骤才能发生(参见文档),所以最简单的方法是用老式的方式做事,只需告诉开发人员通过包的 README 文件创建配置文件。

于 2020-08-15T12:13:21.210 回答