沃梦达 / 编程问答 / php问题 / 正文

每个包一个 parameters.yml,symfony2

A parameters.yml per bundle, symfony2(每个包一个 parameters.yml,symfony2)

本文介绍了每个包一个 parameters.yml,symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以为每个包定义一个 parameters.yml 文件,或者只为需要它的包定义并加载它们.

I was wondering if it's possible to defined a parameters.yml file for every bundle or only for the bundles that need it and load those.

我已经搜索了很多,但我找不到这样的解决方案.

I have searched a lot but i can't find such solution.

推荐答案

你应该澄清一下;您希望每个捆绑包都自动包含一个 parameters.yml 文件吗?恐怕您需要修改 Symfony 的 DI 核心.不过有一个简单的替代方案.

You should clarify a bit; you want every single bundles to automatically include a parameters.yml file? I am afraid you would need to modify Symfony's DI core. There is an easy alternative though.

如果您使用一些 DependencyInjection 创建自己的包,那么您可以在包的扩展类中添加 $loader->load('parameters.yml');.

If you create your own bundle with some DependencyInjection then you can add $loader->load('parameters.yml'); in the bundle's extension class.

扩展类应该位于YourBundle/DependencyInjection/YourBundleExtension.php.

该类应如下所示

class YourBundleExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new LoaderYamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
        $loader->load('parameters.yml'); // Adding the parameters file
    }
}

因此,在这种情况下,parameters.yml 文件将位于 YourBundle/Resources/config/parameters.yml 中.

So in this case the parameters.yml file would be in YourBundle/Resources/config/parameters.yml.

这篇关于每个包一个 parameters.yml,symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:每个包一个 parameters.yml,symfony2

基础教程推荐