我最近遇到了类似的情况。我希望有几个目录都运行相同的应用程序但使用不同的数据库。我是这样做的:
首先我创建了一些空的子目录。在本例中,我们将它们命名为 /subdir_1、/subdir_2 和 /subdir_3。
在每个目录中,我制作了 CodeIgniter index.php 文件的副本并将其放置在每个目录中。我的文件结构现在看起来像:
/application
/system
/subdir_1
index.php
/subdir_2
index.php
/subdir_3
index.php
这也可以通过一些 .htaccess 规则来完成,但那是另一回事了。
在每个 index.php 文件中,我将 $system_path 和 $application_folder 变量更改为指向 /application 和 /system 目录。对于我的特殊情况,我将 index.php 文件更改为:
/*
*---------------------------------------------------------------
* SYSTEM FOLDER NAME
*---------------------------------------------------------------
*
* This variable must contain the name of your "system" folder.
* Include the path if the folder is not in the same directory
* as this file.
*
*/
$system_path = "../system";
/*
*---------------------------------------------------------------
* APPLICATION FOLDER NAME
*---------------------------------------------------------------
*
* If you want this front controller to use a different "application"
* folder then the default one you can set its name here. The folder
* can also be renamed or relocated anywhere on your server. If
* you do, use a full server path. For more info please see the user guide:
* http://codeigniter.com/user_guide/general/managing_apps.html
*
* NO TRAILING SLASH!
*
*/
$application_folder = "../application";
此时点击任何子目录应该会显示您的应用程序。
从这里我们需要为每个目录设置base_href。我们将通过对/application/config/config.php 主文件的简单更改来做到这一点。将 /application/config/config.php 文件中的 $config['base_url'] 行替换为:
$uri = $_SERVER['REQUEST_URI'];
$pieces = explode('/', $uri);
if ($pieces[1] == 'index.php') {
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/';
define('SITE', 'default');
} else {
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $pieces[1] . '/';
define('SITE', $pieces[1]);
}
上面的 sn -p 将$config['base_url'] 设置为适当的子目录(如果不在子目录中,则为根目录)。
define('SITE', $pieces[1]) 行创建了一个我们可以在整个应用程序中访问的常量,让我们知道我们在哪个子目录中。
最后一个难题是让我们的应用程序知道要使用哪个数据库。就我而言,我希望每个子目录都使用自己的数据库。为此,我们将使用在 config.php 文件中创建的 SITE 常量。
在我们的/application/config/database.php 文件中,我们将添加一些备用数据库设置。为此,我们复制[default] 数据库连接设置并为我们的每个子域设置备用设置。以下是我的一套套装的外观:
$db['subdir_1']['hostname'] = "localhost";
$db['subdir_1']['username'] = "[USERNAME]";
$db['subdir_1']['password'] = "[PASSWORD]";
$db['subdir_1']['database'] = "[DATABASE]";
$db['subdir_1']['dbdriver'] = 'mysql';
$db['subdir_1']['dbprefix'] = '';
$db['subdir_1']['pconnect'] = TRUE;
$db['subdir_1']['db_debug'] = TRUE;
$db['subdir_1']['cache_on'] = FALSE;
$db['subdir_1']['cachedir'] = '';
$db['subdir_1']['char_set'] = 'utf8';
$db['subdir_1']['dbcollat'] = 'utf8_general_ci';
$db['subdir_1']['swap_pre'] = '';
$db['subdir_1']['autoinit'] = TRUE;
$db['subdir_1']['stricton'] = FALSE;
我还有 [subdir_2] 和 [subdir_3] 的其他套装。现在我们需要告诉我们的应用程序使用哪些数据库设置。为此,我们获取SITE 常量,因此/application/config/database.php 文件中的最后一行是:
$active_group = (defined('SITE') && array_key_exists(SITE, $db)) ? SITE : 'default';
以上行设置活动数据库设置组以匹配子目录。
就是这样 :) 希望这对某人有所帮助。