Reputation: 521
could i use one codeigniter framework directory to create multiple applications?
I have two website with one CodeIgniter application, i am used one DB for both of website, system folder, config, view, controller etc are also same.
main website is working fine, all links are working and another website only home page is working, rest of the links are not working, when i am open another link the error is occur: "Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid." version of codeigniter is 2.2.0, please help.
Thanks in advance
Upvotes: 1
Views: 2456
Reputation: 4340
I think the answer by @avenda goes someway but as you've mentioned there is only 1x DB then there is no need to play around with that code.
I think your simplest solution in your config file is simply:
$config['base_url'] = $_SERVER['HTTP_HOST'];
And then in your routes file:
if ($_SERVER['HTTP_HOST'] == 'x'){
$routes['default_controller'] = 'xController';
} elseif ($_SERVER['HTTP_HOST'] == 'y'){
$routes['default_controller'] = 'yController';
}
Haven't tested it but have a similar need coming up on a project so will have to implement something similar.
Upvotes: 0
Reputation: 502
Try this:
ex: - subdomain1.hostname.com - subdomain2.hostname.com
then in my front index:
// 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);
}
}
in my config.php config:
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;
}
in my database.php config:
...
$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';
...
finally the constants.php config:
//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;
}
I hope you find it useful
Upvotes: 1