我在localhost上测试我的Codeigniter站点,然后在服务器上更新它。在它们之间切换涉及许多与调整相关的问题。
所以我想只用一个常量来配置它:MYCUSTOM_SERVER_LOCATION
然后根据我的服务器(localhost或myhost)的位置配置我的数据库连接和密码。唯一的问题是database.php在mysettings
库之前运行。即使在配置文件而不是库中进行设置也会产生相同的结果。
[增订]
应用/配置/ autoload.php:
...
$autoload['libraries'] = array('mysettings','database','session');
...
应用/库/ mysettings.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
define("MYCUSTOM_SERVER_LOCATION", "localhost");
class mysettings {
//////////////////////////////////////////////////
// option: localhost
// option: 000webhost
//$config["mycustom_server"]="localhost";
}
应用/配置/ database.php中
if(MYCUSTOM_SERVER_LOCATION=="localhost")
{
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '...';
$db['default']['password'] = '...';
$db['default']['database'] = '...';
}
else if(MYCUSTOM_SERVER_LOCATION=="myserver")
{
$db['default']['hostname'] = '...';
$db['default']['username'] = '...';
$db['default']['password'] = '...';
$db['default']['database'] = '...';
}
else
{
echo "Unknown server.";
}
输出结果:
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant MYCUSTOM_SERVER_LOCATION - assumed 'MYCUSTOM_SERVER_LOCATION'
Filename: config/database.php
Line Number: 51
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant MYCUSTOM_SERVER_LOCATION - assumed 'MYCUSTOM_SERVER_LOCATION'
Filename: config/database.php
Line Number: 58
Unknown server.
答案 0 :(得分:0)
您可以在$active_group
application/config/database.php
变量
使用示例:
/*
The $active_group variable lets you choose which connection group to
make active. By default there is only one group (the 'default' group).
*/
$active_group = "development";
$db['development']['hostname'] = "localhost";
$db['development']['username'] = "us";
$db['development']['password'] = "";
$db['development']['database'] = "db1";