问题描述
我想知道是否可以从另一个包修改包配置.比方说,例如,我正在使用具有以下配置的 FOSUserBundle:
I was wondering whether there's a possible to modify a bundles configuration from another bundle. Let's say, for example, I'm using the FOSUserBundle with the following configuration:
fos_user:
db_driver: orm
firewall_name: main
user_class: AcmeUserBundleEntityUser
现在,我想在加载特定扩展(AcmeFoobarExtension
)时更改用户类.加载时是否可以更改配置AcmeFoobarExtension
?例如:
And now, I want to change the user class when loading a specific extension (the AcmeFoobarExtension
). Is it possible to change the configuration when loadingthe AcmeFoobarExtension
? For example:
<?php
namespace AcmeFoobarBundleDependencyInjection;
// use statements for dependency injection
class FoobarExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$container->setConfiguration(
'fos_user.user_class',
'AcmeFoobarBundleEntityUser'
);
}
}
这样的事情可能吗?还是破坏了依赖注入的目的?
Is something like this possible? Or defeats it the purpose of dependency injection?
显然 Symfony 2.2 有一个 pull request 解决了这个想法/问题.
Apparently there's a pull request for Symfony 2.2 which addresses this idea/issue.
编辑 2:现在在 Symfony 2.2 中可用(在 文章中食谱).
EDIT 2: This is now available in Symfony 2.2 (with an article in the cookbook).
推荐答案
需要注意的是,现在这是 Symfony 的官方功能:
It should be noted that this is now an official feature of Symfony:
http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html
bundle 可以将配置值添加到全局配置参数中.为此,捆绑扩展必须实现 PrependExtensionInterface
.prepend()
方法然后可以添加全局配置值.
A bundle can prepend configuration values to the global config parameters. To do that, the bundle extension must implement PrependExtensionInterface
. The prepend()
method can then add global configuration values.
但是请注意,这些值可能会被其他包和配置文件本身覆盖.
Note, however, that the values may be overwritten by other bundles and in the config file itself.
这篇关于从另一个包修改包配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!