简体   繁体   English

Bundle 的 Symfony 4 自定义配置 yaml 文件

[英]Symfony 4 Custom config yaml file for Bundle

I'm trying to convert a bundle to symfony 4 and need to update my ancient parameters.yml to the modern symfony 4 way of life.我正在尝试将一个包转换为 symfony 4,并且需要将我古老的 parameters.yml 更新为现代 symfony 4 的生活方式。 Basicall the bundle itself - shared across multiple apps - should have a configurable file under /config/packages/. Basicall 包本身 - 在多个应用程序之间共享 - 应该在 /config/packages/ 下有一个可配置的文件。

However I receive this error:但是我收到此错误:

(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 /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 /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 /config/packages/ptmr.yaml

ptmr:
  twitter:
    client_id: 123
    client_secret: your_secret

-- Note: The Bundle itself works. -- 注意:Bundle 本身有效。

I added this line to psr-4 in composer.json :我在composer.json的 psr-4 中添加了这一行:

    "PtmrBundle\\": "PtmrBundle/"

This lines to config/routes/ annotations.yml这行到 config/routes/ annotations.yml

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

These lines to config/ services.yaml这些行到 config/ services.yaml

services:
    ...

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

    ...

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

And of course PtmrBundle/PtmrBundle.php当然还有 PtmrBundle/PtmrBundle.php

<?php

namespace PtmrBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class PtmrBundle extends Bundle
{

}

I'm following these instructions and I reaaaly do not see any errors.我正在按照这些说明进行操作,我真的没有看到任何错误。 What am I missing?我错过了什么? Symfony 4.2.交响乐 4.2。

I found the answer after all.我终于找到了答案。 To make your own config/bundle.yaml parameters file, simply do:要制作您自己的config/bundle.yaml参数文件,只需执行以下操作:

Step 1: Create a file in your bundle DependencyInjection/{BundleNameWithoutBundle}Extension.php , eg for MyBundle > MyExtension.php第 1 步:在您的包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);
    }
}

Also see How to Load Service Configuration inside a Bundle另请参阅如何在捆绑包中加载服务配置

Step 2 : Make a configuration file that provides a schema for your.yaml file DependencyInjection/Configuration.php第 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;
    }
}

Also see How to Create Friendly Configuration for a Bundle另请参阅如何为捆绑包创建友好配置

Step 3 : Mirror the Configuration.php in your config/my.yaml第 3 步:在您的config/my.yaml中镜像Configuration.php

my:
  project_name: "My Name"

Now the parameter my is available (set in MyExtension Class).现在参数my可用(在 MyExtension 类中设置)。 Simply get it in your Controller like:只需在您的控制器中获取它,例如:

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

}

Note: Most bundles go further and manipulate the bundles config xml files, which is out of scope for a simple question like this.注意:大多数 bundle 更进一步操作 bundle 配置 xml 文件,这超出了像这样的简单问题的范围。 A simple example on how to do this is the KnpPaginatorBundle which is not overly complicated to understand for settings parameters.关于如何执行此操作的一个简单示例是KnpPaginatorBundle ,它对于设置参数的理解并不过分复杂。

Personal Note: It seems to me, the Symfony docs are overly complicated and should provide a simple example.个人注意事项:在我看来,Symfony 文档过于复杂,应该提供一个简单的示例。 You got to know a lot of nomenclature and it's hard to learn it, especially compared to other well documented symfony chapters.你必须了解很多术语,但很难学会,尤其是与其他有据可查的 symfony 章节相比。

You are attempting to load configuration for your bundle before Symfony is aware of your bundle.你正试图在 Symfony 知道你的包之前加载你的包的配置。 Your bundle class must be loaded and configured before you can parse configuration under the ptmr property.在解析ptmr属性下的配置之前,必须先加载和配置您的捆绑包类。

Typically your bundle would load __DIR__.'/../Resources/config/services.yaml which contains service and parameters definitions.通常,您的包会加载__DIR__.'/../Resources/config/services.yaml ,其中包含serviceparameters定义。 Outside of your bundle, in your application config/ dir, you would then load configuration under the ptmr property.在你的包之外,在你的应用程序config/目录中,你将在ptmr属性下加载配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM