如何从第三个包覆盖第三个包的配置 [英] How to override configuration of third bundle from a third bundle

查看:64
本文介绍了如何从第三个包覆盖第三个包的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a bundle in Symfony 4, but in my bundle I use FOSUserBundle. So I would like to defined configuration for FOSUserBundle from my own bundle and not from the main configuration. How to do it ? It doesn't recognize the node

没有能够加载fos_user"配置的扩展(在 C:\wamp64\www\MyProject\src\CMSBundle\DependencyInjection/../Resources/config\fos_user.yaml).寻找命名空间fos_user",没有找到

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) 的配置

And if I load the configuration of FOSUserBundle (FOS\UserBundle\DependencyInjection/Configuration.php) in my CMSExtension.php

我收到这条消息

必须配置路径fos_user"处的子节点db_driver".

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 类中,你可以实现 PrependExtensionInterface 并添加一个 prepend方法.

In your CMSExtension class, you can implement the PrependExtensionInterface and add a prepend method.

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

In this method, you'll be able to override the FOSUserBundle configuration:

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

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

查看 Symfony 文档中的此页面.

Look a this page in the Symfony documentation.

这篇关于如何从第三个包覆盖第三个包的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆