1

我正在尝试将捆绑包转换为 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。

4

2 回答 2

0

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

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

于 2019-04-16T03:41:17.393 回答
0

毕竟我找到了答案。要制作自己的config/bundle.yaml参数文件,只需执行以下操作:

第 1 步:在您的 bundle 中创建一个文件DependencyInjection/{BundleNameWithoutBundle}Extension.php,例如 MyBundle > MyExtension.php

<?php
namespace MyBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

class MyExtension extends \Symfony\Component\DependencyInjection\Extension\Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('my', $config);
    }
}

另请参阅如何在 Bundle 中加载服务配置

第 2 步:制作一个配置文件,为您的 .yaml 文件提供架构DependencyInjection/Configuration.php

<?php

namespace MyBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('my');

        $treeBuilder->getRootNode()
            ->children()
                ->variableNode('project_name')->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

另请参阅如何为捆绑包创建友好配置

第 3 步:镜像Configuration.php你的config/my.yaml

my:
  project_name: "My Name"

现在参数my可用(在 MyExtension 类中设置)。只需将它放在您的控制器中,例如:

class IndexController extends AbstractController
{
    public function indexAction(ParameterBagInterface $parameterBag)
    {
        ...
        dump($parameterBag->get('ptmr'));
        return $this->render('index.html.twig');
    }

}

注意:大多数捆绑包更进一步并操作捆绑包配置 xml 文件,这超出了像这样的简单问题的范围。如何做到这一点的一个简单示例是KnpPaginatorBundle,它对于设置参数的理解并不太复杂。

个人注意:在我看来,Symfony 文档过于复杂,应该提供一个简单的示例。你要知道很多命名法,而且很难学习,尤其是与其他有据可查的 symfony 章节相比。

于 2020-03-06T10:01:24.740 回答