将参数加载到 Symfony 包时出错

Error when loading parameters into Symfony bundle

我收到以下错误:

There is no extension able to load the configuration for "upload_images" (in "/var/www/vhosts/diabetigraph-dev/vendor/verzeilberg/upload-images/src/Resources/services.yaml"). Looked for namespace "upload_images", found "none"

这是我的文件:

services.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;
    }
}

UploadImagesExtension

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

我做错了什么?

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

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

在您的包中,Configuration 对象表示包的配置。您的包中没有 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 使用作曲家食谱来简化捆绑包的安装。该秘诀不仅将包添加到 config/bundles.php,还将任何默认配置文件复制到 config/packages。不幸的是,此复制需要额外的步骤(请参阅文档),因此最简单的做法是 old-fashion 方式,只需告诉开发人员通过捆绑包的自述文件创建配置文件即可。