【问题标题】:one codeigniter application folder run two website with different domain一个 codeigniter 应用程序文件夹运行两个不同域的网站
【发布时间】:2014-12-19 07:57:35
【问题描述】:

我可以使用一个 codeigniter 框架目录来创建多个应用程序吗?

我有两个网站和一个 CodeIgniter 应用程序,我使用一个数据库用于网站、系统文件夹、配置、视图、控制器等。

主网站工作正常,所有链接都工作,另一个网站只有主页工作,其余链接不工作,当我打开另一个链接时出现错误:“无法加载您的默认控制器。请确保您的 Routes.php 文件中指定的控制器有效。” codeigniter的版本是2.2.0,请帮忙。

提前致谢

【问题讨论】:

  • 你解决了吗?

标签: codeigniter routes


【解决方案1】:

试试这个:

例如: - subdomain1.hostname.com - subdomain2.hostname.com

然后在我的前面索引:

// Host address
$host = $_SERVER['HTTP_HOST'];
$SERVER_NAME = $_SERVER['SERVER_NAME'];
define('HOST', $SERVER_NAME);

// Extract eventual subdomain
$host3 = strrpos($host, '.');
$host2 = substr($host, 0, $host3);
$host1 = strrpos($host2, '.');
$subdomain = substr($host, 0, $host1);

if($host == 'localhost') {
    define('HOST_TYPE', 'localhost');
    define('SUBDOMAIN', FALSE);
} else {
    if($subdomain !== 'www' && $subdomain !== '') {
        define('HOST_TYPE', 'subdomain');
        define('SUBDOMAIN', $subdomain);
    } else {
        define('HOST_TYPE', 'site');
        define('SUBDOMAIN', FALSE);
    }
}

在我的 config.php 配置中:

switch (HOST_TYPE) {
    case 'subdomain':
        $config['base_url'] = 'http://'.SUBDOMAIN.'.hostname.com/';
        break;
    case 'localhost':
        $config['base_url'] = 'http://localhost/devpage/';
        break;
    default:
        //$config['base_url']   = 'http://mainpage.com/';
        break;
}

在我的 database.php 配置中:

...
$db['default']['hostname'] = 'localhost';
switch (HOST_TYPE) {
    case 'subdomain':

        $username;
        $password;
        $con=mysqli_connect("localhost","username_db","password_db","name_db");
        $query = "SELECT * FROM subdomain_table WHERE subdomain_name = '".SUBDOMAIN."';" or die("Error in the consult.." . mysqli_error($con)); 
        if ($result = mysqli_query($con, $query)) {
            while($row = mysqli_fetch_array($result)) { 
              $username = $row["username"];
              $password = $row["password"];
            } 
        }else{
            mysqli_close($con);
            redirect('http://hostname.com'); //No sudomain found or error page
        }
        // Check connection
        if (mysqli_connect_errno()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
                mysqli_close($con);
        }else{

        }
        mysqli_close($con);
        $db['default']['database'] = 'db_prefix'.SUBDOMAIN;
        $db['default']['username'] = 'username_prefix'.$usuario;
        $db['default']['password'] = $password;
        break;
    case 'localhost':
        $db['default']['database'] = 'local_dev_db_name';
        $db['default']['username'] = 'some_username';
        $db['default']['password'] = 'some_password';
        break;
    default:
        redirect('http://mainpage.com');
        break;
}



$db['default']['dbdriver'] = 'mysql';
...

最后是 constants.php 配置:

//this one resolve all your links!!!!!
switch (HOST_TYPE) {
    case 'subdomain':
        define('URL','http://'.SUBDOMAIN.'.hostname.com/');
        break;
    case 'localhost':
        define('URL','http://localhost/devpage/');
        break;
    default:
        define('URL','http://mainpage.com/');
        break;
}

希望对你有用

【讨论】:

    【解决方案2】:

    我认为@avenda 的答案在某种程度上是可行的,但正如你所提到的,只有 1x DB,因此无需使用该代码。

    我认为您在配置文件中最简单的解决方案是:

    $config['base_url'] = $_SERVER['HTTP_HOST'];
    

    然后在你的路由文件中:

    if ($_SERVER['HTTP_HOST'] == 'x'){
        $routes['default_controller'] = 'xController';
    } elseif ($_SERVER['HTTP_HOST'] == 'y'){
        $routes['default_controller'] = 'yController';
    }
    

    尚未对其进行测试,但在项目中有类似的需求,因此必须实施类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-25
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      相关资源
      最近更新 更多