捆绑包的Symfony 4自定义配置yaml文件

时间:2019-04-15 15:31:13

标签: php symfony symfony-4.2

我正在尝试将捆绑软件转换为symfony 4,并且需要将我的古老的parameter.yml更新为现代的symfony 4生活方式。 Basicall捆绑包本身-在多个应用程序之间共享-应该在/ 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

- 注意: 捆绑包本身可以工作。

我将此行添加到 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。

2 个答案:

答案 0 :(得分:0)

您正在尝试在Symfony意识到捆绑软件之前加载捆绑软件的配置。在解析ptmr属性下的配置之前,必须先加载并配置了捆绑软件类。

通常,您的分发包会加载__DIR__.'/../Resources/config/services.yaml,其中包含serviceparameters的定义。在包的外部,在应用程序config/的目录下,您将在ptmr属性下加载配置。

答案 1 :(得分:0)

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

步骤1:,例如,在捆绑包DependencyInjection/{BundleNameWithoutBundle}Extension.php中创建一个文件。 for 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);
    }
}

另请参见How to Load Service Configuration inside a 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;
    }
}

另请参见How to Create Friendly Configuration for a Bundle

第3步:将Configuration.php中的config/my.yaml镜像到

my:
  project_name: "My Name"

现在参数my可用(在MyExtension Class中设置)。只需像下面这样在控制器中获取它即可:

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

}

注意:大多数捆绑软件会更进一步,对捆绑软件配置xml文件进行操作,这对于像这样的简单问题是不可行的。一个简单的示例是KnpPaginatorBundle,对于设置参数来说,理解起来并不太复杂。

个人说明:在我看来,Symfony文档过于复杂,应提供一个简单示例。您必须了解很多术语,而且很难学习,尤其是与其他有据可查的symfony章节相比。