Symfony4 捆绑包创建 - 无法生成配置文件

问题描述 投票:0回答:2

此问题的代码演示位于:GitHubPackagist,请参阅 v0.0.3。

我的问题:我想在运行时生成 config\packages\owner_test.yaml 文件:

作曲家要求所有者/测试包:0.0.3

但是 Composer 显示错误:

没有扩展能够加载“owner_test”的配置(在“C:\Users\USER\PROJECT endor\owner est-bundle\DependencyInjection/../Resources/config\owner_test.yaml”中)。查找命名空间“owner_test”,发现“无”。

根密钥和捆绑包名称是正确的,我已阅读找到的所有文档,但没有任何内容是正确的:

感谢您的帮助

php symfony config
2个回答
0
投票

这是一个工作示例:https://github.com/cerad/bundle4

捆绑包源位于 src-owner 目录中

您缺少并且可能没有完全理解的是捆绑包的 Configuration 类:

namespace Owner\TestBundle\DependencyInjection;

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

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('owner_test');
        $treeBuilder->getRootNode()
            ->children()
                ->booleanNode('isDisplay')->defaultTrue()->end()
                ->booleanNode('someBoolean')->defaultTrue()->end()
            ->end()
        ;
        return $treeBuilder;
    }
}

Configuration 类用于为您的包设置默认配置值。 这些值可以通过 config/packages/owner_test.yaml 在应用程序级别覆盖。 您的捆绑包不会有自己的配置 yaml 文件。 事情不是这样的。

您的分机将变为:

namespace Owner\TestBundle\DependencyInjection;

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

class OwnerTestExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        // Has what is in config/packages/owner_test.yaml
        dump($configs);

        $configuration = new Configuration();

        $config = $this->processConfiguration($configuration, $configs);
        dump($config);

        // At this point you would use $config to define your parameters or services

    }
}

研究转储输出并阅读文档,直到您了解处理配置时发生的情况。 只需使用“bin/console 缓存:清除”即可。 我添加了第二个配置值只是为了使其更容易理解。 您可能想添加更多。

此时,您需要添加代码以将配置值传输到 ContainerBuilder。


0
投票

好的,正在运行,非常感谢;)

我不确定这是最佳实践,如果需要的话你可以纠正我吗?

您的代码非常完美,为了进行测试,我只需添加:

测试配置变量:

#config\services.yaml
parameters:
    testParam: 42

测试环境变量:

#.env
SECRET_TEST_KEY=abcde

文件背面的这一行用于将捆绑配置发送到应用程序:

#src-owner\DependencyInjection\OwnerTestExtension.php
$container->setParameter('owner_test', $config);

PHP 脚本(添加只是为了模拟真实的脚本构造):

#src-owner\Services\OwnerTestService.php
<?php

namespace Owner\TestBundle\Services;

class OwnerTestService
{
    private $config;

    public function __construct(array $config)
    {
        $this->config = $config;
    }

    public function getBundleConfig()
    {
        return $this->config['someBoolean'];
    }
    function getAppConfig()
    {
        return  $this->config['testParam'];
    }
}

用于运行测试的控制器:

#src\Controller\TestController.php
namespace App\Controller;

use Owner\TestBundle\Services\OwnerTestService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestController extends AbstractController
{
    /**
     * @Route("/")
     */
    public function show()
    {
        $config = array_merge(['testParam' => $this->getParameter('testParam')], $this->getParameter('owner_test'));
        $ownerTestService = new OwnerTestService($config);
        return new Response(
            '<html><body>BUNDLE CONFIG : ' . $ownerTestService->getBundleConfig() . ', APP CONFIG : ' . $ownerTestService->getAppConfig()  .  ', ENV : ' . $_ENV['SECRET_TEST_KEY'] . '</body></html>'
        );
    }
}

结果很完美,我可以在捆绑包中使用我的所有配置(和环境):)

© www.soinside.com 2019 - 2024. All rights reserved.