在 Symfony2 的 config.yml 中,您可以添加“导入”,例如:
imports:
- { resource: services.yml }
在我的 services.yml 中,我有:
imports:
security_bundle:
resource: @AcmeSecurityBundle/Resources/config/services.yml
但是,为捆绑声明服务的另一种方法是使用
DependencyInjection
Extension
因此无需手动将任何内容导入 config.yml 从而解耦代码。namespace Acme\Bundle\SecurityBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class AcmeSecurityExtension extends Extension {
public function load(array $configs, ContainerBuilder $container) {
$loader = new YamlFileLoader(
$container, new FileLocator(__DIR__ . '/../Resources/config')
);
$loader->load('services.yml');
}
}
问题
这适用于服务声明,但例如你想要一个包来配置另一个包,比如添加
LiipImagineBundle
(就像 AvalancheImagineBundle
)过滤器:liip_imagine:
filter_sets:
security_avatar_thumbnail:
quality: 75
filters:
thumbnail: { size: [140, 140], mode: inset }
Symfony 然后提示说
There is no extension able to load the configuration for "liip_imagine"
那么有没有人知道是否有办法从另一个包中为第三方包添加配置而不触及
config.yml
?
最佳答案
在 Symfony 2.2 中,可以借助 PrependExtensionInterface。
看看“如何简化多个 Bundle 的配置”cookbook 条目:
http://symfony.com/doc/current/cookbook/bundles/prepend_extension.html
关于Symfony2 : Is it possible to add configuration for another bundle via DependencyInjection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9285665/