如何让Cakephp 3通过Apache环境变量选择数据库连接 [英] How to let Cakephp 3 choose database connection by Apache environment variable
问题描述
我正在使用cakephp v3,并希望在两种不同的环境中安装该应用程序,一种用于开发,另一种用于生产.两种安装都应包含完全相同的文件(和文件内容),因此我可以使用'git'或'svn'轻松部署应用程序.
I'm working with cakephp v3 and want to install the application in two different environments, one for development and one for production use. Both installations should consist of exactly the same files (and file contents), so I could use 'git' or 'svn' to easily deploy the application.
如果两个环境都托管在同一台计算机上,则我需要不同的数据库设置(以便开发环境使用其自己的测试"数据库).我想到了在app.php
中配置两个数据源",默认"一个用于生产,一个开发".
If both environments are hosted on the same machine, I need different database settings (so that the development env uses its own 'testing' DB). I thought of configuring two 'Datasources' in app.php
, the 'default' one for production and a `development'.
但是如何在两个来源之间切换?
But how can I switch between both sources?
更具体地说:目前,我在Apache配置中为开发环境定义以下环境变量:
To be more specific: Currently I define the following environment variable in my Apache config for the development environment:
SetEnv CAKEPHP_DEBUG 1
然后我像这样在app.php
文件中更改了'debug'的定义:
Then I changed the definition of 'debug' in the app.php
file like this:
'debug' => (bool)getenv('CAKEPHP_DEBUG'),
这仅在开发计算机上启用调试模式.现在,我也想以同样简单的方式切换数据库配置.
This enables DEBUG mode only on the development machine. Now I also want to switch database configuration in the same easy way.
(我已经找到了cakephp v2的一些解决方案,但是所有解决方案都已经很老了,我不确定在cakephp v3中实现它的最佳方法是什么.)
(I already found some solutions for cakephp v2, but all of them are pretty old and I'm not sure what's the best way to do it in cakephp v3.)