将参数加载到 Symfony 包时出错 [英] Error when loading parameters into Symfony bundle

查看:33
本文介绍了将参数加载到 Symfony 包时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

没有能够加载配置的扩展上传图片"(在/var/www/vhosts/diabetigraph-dev/vendor/verzeilberg/upload-images/src/Resources/services.yaml").寻找命名空间upload_images",找到none"

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

上传图片扩展

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 配置需要移动到它自己的应用程序级配置/包文件:

The basic answer is that your upload_images configuration needs to be moved to it's own application level config/packages file:

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

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

In your bundle, the Configuration object represents the bundle's configuration. There is no upload_images.yaml sort of file in your bundle. The object is quite powerful so you can add defaults and options and what not.

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

Your bundle's extension takes care of processing the final configuration and making information such as parameters available to the rest of the system:

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

这可能令人困惑,至少对我来说,我不得不多次阅读文档并进行大量实验以了解整个过程.

It can be confusing and for me at least I had to read the docs multiple times and do quite a bit of experimenting to understand the overall process.

这种混乱是 Symfony 从每个应用程序的多个捆绑包演变为一个应用程序捆绑包,再到根本没有应用程序捆绑包的原因之一.

The confusion is one of the reasons that Symfony evolved from multiple bundles per application to one application bundle to no application bundle at all.

我还可以添加 Symfony 使用作曲家配方来简化安装包.该配方不仅将包添加到 config/bundles.php,而且还将任何默认配置文件复制到 config/packages.不幸的是,此副本需要额外的步骤(请参阅文档),因此最简单的方法是使用老式方法,只需告诉开发人员通过包的 README 文件创建配置文件即可.

I might also add the Symfony uses composer recipes to simplify installing a bundle. The recipe not only adds the bundle to config/bundles.php but copies any default configuration file to config/packages as well. Unfortunately, there are additional steps required for this copy to happen (see the docs) so it's easiest to do things the old-fashion way and just tell the developer to create the config file via the bundle's README file.

这篇关于将参数加载到 Symfony 包时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆