Symfony2:如何为每个主机名使用不同的参数(.ini)? [英] Symfony2: How can I use different parameters(.ini) per host name?

查看:20
本文介绍了Symfony2:如何为每个主机名使用不同的参数(.ini)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony 在 /app/config/parameters.ini 中保存配置参数.

Symfony holds configuration parameters in /app/config/parameters.ini.

我想根据主机名使用不同的参数集.最初,主机名将决定要使用的数据库,但很可能会扩展以涵盖更多.

I'd like to to use different sets of parameters based on the hostname. Initially the hostname will determine the database to use but may well expand to cover more.

我更喜欢将每个主机的参数存储在一个单独的文件中,以便以编程方式轻松生成.

I'd prefer that the parameters per host are stored in a separate file so as to make this easy to programmatically generate.

从概念上讲,我想像这样存储配置参数:

Conceptually I'd like to store configuration parameters like this:

/app/config/parameters.ini
/app/config/foo.example.com.parameters.ini
/app/config/bar.example.com.parameters.ini

我看到在 \Sensio\Bundle\DistributionBundle\Controller\ConfigurationController 中引用了 /app/config/parameters.ini 并且修改这个文件应该可以工作.

I see that /app/config/parameters.ini is referenced in \Sensio\Bundle\DistributionBundle\Controller\ConfigurationController and that modifying this file should work.

这是最好的方法吗?有没有更直接的方法,不需要打补丁核心框架?

Is this be best approach? Is there a more straightforward approach that does not require patching the core framework?

推荐答案

我会考虑不同的环境对一个 ini 文件使用不同的部分.您可以使用以 prod1 为前缀的参数和具有相同前缀的 prod2 的 prod1 环境:

I would think about different environments which use different parts for one ini file. You could have a prod1 environment using parameters prefixed with prod1 and a prod2 with the same:

参数.ini:

[parameters]
  prod1_database_driver = pdo_mysql
  prod1_database_host = 127.0.0.1

  # ...

  prod2_database_driver = pdo_mysql
  prod2_database_host = localhost

它们都使用 prod.yml 配置,但会覆盖您想从 parameters.ini 中读取的内容:

They both use the prod.yml configuration but overwrite the stuff you want to read from the parameters.ini:

config_prod1.yml:

config_prod1.yml:

imports:
    - { resource: config_prod.yml }

// .. overwrite stuff here

通过这种方式,您还可以解决缓存问题,因为您已经针对每个环境进行了缓存.

This way you also get around the caching issue, as you already have on cache per environment.

要将两者分开,可以像在开发环境中一样创建和使用 app_prod1.php 和 app_prod2.php,或者根据 app.yml 中的主机更改环境.

To seperate the two, either create and use a app_prod1.php and app_prod2.php as you would with the dev environment or change the environment depending on the host in you app.yml.

这篇关于Symfony2:如何为每个主机名使用不同的参数(.ini)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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