如何让Cakephp 3通过Apache环境变量选择数据库连接 [英] How to let Cakephp 3 choose database connection by Apache environment variable

查看:112
本文介绍了如何让Cakephp 3通过Apache环境变量选择数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.)

推荐答案

您可以在配置中定义任意数量的连接 文件.您还可以在运行时使用以下命令定义其他连接 Cake \ Datasource \ ConnectionManager :: config().

You can define as many connections as you want in your configuration file. You can also define additional connections at runtime using Cake\Datasource\ConnectionManager::config().

所以我想您可以在AppController beforeFilter中检查debug的值并更改默认数据库连接

So I guess you can check the value of debug in AppController beforeFilter and change the default database connection

AppController.php

if(Configure::read('debug') == 1)
{
    ConnectionManager::config('default', [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'dev_server',
        'username' => 'dev_username',
        'password' => 'dev_passwd',
        'database' => 'development',
        'encoding' => 'utf8',
        'timezone' => 'UTC',
        'cacheMetadata' => true,
    ]);
}

我认为您可以使用三元运算符在app.php中做类似的事情

I think you can do something similar in app.php using the ternary operator

app.php

'Datasources' => [
    'default' => getenv('CAKEPHP_DEBUG')== 1 ? [ /* debug params */ ] : [ /* default params */]
    ...
]

但是以某种方式,这似乎不是干净"的方法

But somehow it don't seem the 'clean' way to do it

我认为更干净的方法是在app.php中设置两个配置,然后在appController中选择要使用的配置

I think that a cleaner way would be to set both configurations in app.php and then in appController choose what configurations to use

app.php

'Datasources' => [
    'debug' => [ /* debug params */ ],
    'default' => [ /* default params */]
]

表格文件

public static function defaultConnectionName() {
    if(Configure::read('debug') == 1)
        return 'debug';
    return 'default';
}

这篇关于如何让Cakephp 3通过Apache环境变量选择数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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