如何从第三个包覆盖第三个包的配置

How to override configuration of third bundle from a third bundle

我在 Symfony 4 中制作一个包,但在我的包中我使用 FOSUserBundle。 所以我想从我自己的包而不是主配置中为 FOSUserBundle 定义配置。怎么做 ?它不识别节点

There is no extension able to load the configuration for "fos_user" (in C:\wamp64\www\MyProject\src\CMSBundle\DependencyInjection/../Resources/config\fos_user.yaml). Looked for namespace "fos_user", found none

如果我在 CMSExtension.php

中加载 FOSUserBundle (FOS\UserBundle\DependencyInjection/Configuration.php) 的配置

我收到这条消息

The child node "db_driver" at path "fos_user" must be configured.

结构

/src
    /CMSBundle
        /Controller
        /DependencyInjection
            CMSExtension.php
        /Entity
        /Repository
        /Resources
            /config
                fos_user.yaml
                routing.yaml
                security.yaml
                services.yaml
        CMSBundle.php

src/CMSBundle/DependencyInjection/CMSExtension.php

class CMSExtension extends Extension {

    public function load(array $configs, ContainerBuilder $container) {
        $loader = new YamlFileLoader(
            $container,
            new FileLocator(__DIR__ . '/../Resources/config')
        );
        $loader->load('services.yaml');
        $loader->load('security.yaml');
        $loader->load('fos_user.yaml');
    }

}

src/CMSBundle/Resourcers/config/fos_user.yaml

fos_user:
    db_driver: orm # other valid values are 'mongodb' and 'couchdb'
    firewall_name: main
    user_class: src\CMSBundle\Entity\User
    from_email:
        address: "%mailer_user%"
        sender_name: "%mailer_user%"

在您的 CMSExtension class 中,您可以实现 PrependExtensionInterface 并添加一个 prepend 方法。

在此方法中,您将能够覆盖 FOSUserBundle 配置:

/**
 * {@inheritdoc}
 */
public function prepend(ContainerBuilder $container)
{
    $newConfig = [
        'db_driver' => '',
        ...
    ];

    $container->prependExtensionConfig('fos_user', $newConfig);
}

在 Symfony 文档中查看 this page