2013-06-21 18 views
0

我在本地主机上测试Codeigniter站点,然后在服务器上更新它。它们之间的切换涉及很多与调整有关的问题。CodeIgniter在数据库设置之前运行库

所以我想只有一个常数的配置吧:MYCUSTOM_SERVER_LOCATION

然后我的数据库连接,密码是根据我的服务器(本地主机或为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

您是否尝试过使用挂钩?我认为这是你需要为你的问题http://ellislab.com/codeigniter/user-guide/general/hooks.html – tomexsans

+0

由于在常量前使用$更新以前的错误。 @tomexsans我不知道钩子。他们在这种情况下有帮助吗? (无需触摸系统文件夹) – gerrnar

+0

@germar一个简单的开关就可以做到这一点。 activerecord变量是CI知道要加载的数据库设置的位置。 – tomexsans

回答

0

可以设置在application/config/database.php

示例用法的$active_group变量:

/* 
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"; 
相关问题