2013-06-21 45 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"; 
相關問題