首页 > 解决方案 > Symfony 4 Bundle 的自定义配置 yaml 文件

问题描述

我正在尝试将捆绑包转换为 symfony 4,并且需要将我古老的 parameters.yml 更新为现代 symfony 4 的生活方式。基本上,捆绑包本身——在多个应用程序之间共享——应该在 /config/packages/ 下有一个可配置文件。

但是我收到此错误:

(1/1) InvalidArgumentException

There is no extension able to load the configuration for "ptmr" (in /var/www/html/ptmr/pws_ptmrio_dev/PtmrBundle/DependencyInjection/../../config/packages/ptmr.yaml). Looked for namespace "ptmr", found none

/PtmrBundle/DependencyInjection/ PtmrExtension.php

<?php
namespace PtmrBundle\DependencyInjection;

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

class PtmrExtension extends Extension
{

    public function load(array $configs, ContainerBuilder $container)
    {

        $configuration = new Configuration(true);
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__ . '/../../config/packages')
        );

        $loader->load('ptmr.yaml');

    }


}

/PtmrBundle/DependencyInjection/Configuration.php _

<?php
namespace PtmrBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{

    private $debug;

    public function  __construct($debug = true)
    {
        $this->debug = (bool) $debug;
    }

    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('ptmr');

        $treeBuilder->getRootNode()
            ->children()
            ->arrayNode('twitter')
            ->children()
            ->integerNode('client_id')->end()
            ->scalarNode('client_secret')->end()
            ->end()
            ->end() // twitter
            ->end()
        ;

        return $treeBuilder;
    }
}

/config/packages/ptmr.yaml _

ptmr:
  twitter:
    client_id: 123
    client_secret: your_secret

-- 注意:Bundle 本身可以工作。

我将此行添加到composer.json中的 psr-4 :

    "PtmrBundle\\": "PtmrBundle/"

这行到 config/routes/ annotations.yml

ptmr_bundle:
    resource: ../PtmrBundle/Controller/
    type: annotation

这些行到 config/ services.yaml

services:
    ...

    PtmrBundle\:
        resource: '../PtmrBundle/*'
        exclude: '../PtmrBundle/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    ...

    PtmrBundle\Controller\:
        resource: '../PtmrBundle/Controller'
        tags: ['controller.service_arguments']

当然还有 PtmrBundle/PtmrBundle.php

<?php

namespace PtmrBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class PtmrBundle extends Bundle
{

}

我正在按照这些说明进行操作,并且我确实没有看到任何错误。我错过了什么?Symfony 4.2。

标签: phpsymfonysymfony-4.2

解决方案


你试图在 Symfony 知道你的 bundle 之前为你的 bundle 加载配置。必须先加载和配置您的捆绑包类,然后才能解析该ptmr属性下的配置。

通常,您的捆绑包将加载__DIR__.'/../Resources/config/services.yaml包含serviceparameters定义的内容。在您的包之外,在您的应用程序config/目录中,您将在ptmr属性下加载配置。


推荐阅读