Error when loading parameters into Symfony bundle

雨燕双飞 提交于 2021-01-29 06:42:39

问题


I get following error:

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"

This are my files:

services.yaml

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

upload_images:
  version: 100

Configuration

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

What am I doing wrong?


回答1:


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

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.

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.

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.



来源:https://stackoverflow.com/questions/63409920/error-when-loading-parameters-into-symfony-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!