-
Raks3010
Newbie
-
Posts: 4
Threads: 2
Joined: Dec 2022
Reputation:
1
01-03-2023, 06:02 AM
(This post was last modified: 01-03-2023, 06:03 AM by Raks3010.)
Hello,
I develop an app with CI4.
I would like to duplicate my application for different business centers without having to install codeigniter each time.
Example :
center1.myapp.com
center2.myapp.com
center3.myapp.com
...
Depending on the name of the subdomain, it loads a different database.
Do you have an idea how to do it?
Thanks in advance !
-
Raks3010
Newbie
-
Posts: 4
Threads: 2
Joined: Dec 2022
Reputation:
1
Right !
I have use this solution.
I generate a json with all my app.
Code: {
"https://center1.myapp.com/": {
"id": "2",
"name": "Center 1",
"url": "https:\/\/center1.myapp.com\/",
"db_host": "localhost",
"db_database": "centre_1",
"db_username": "root",
"db_password": "root",
"created_at": "2023-01-03 12:27:53",
"updated_at": "2023-01-03 14:56:52",
"deleted_at": null,
"active": "1"
},
"https://center2.myapp.com/": {
"id": "4",
"name": "Kraainem",
"url": "https:\/\/center2.myapp.com\/",
"db_host": "localhost",
"db_database": "centre_2",
"db_username": "root",
"db_password": "root",
"created_at": "2023-01-03 14:49:55",
"updated_at": "2023-01-03 14:57:03",
"deleted_at": null,
"active": "1"
},
"Default": {}
}
I have edited this file "public/index.php" with this.
PHP Code: ///////////////////////////////////// ////// MULTI BUSINESS CENTER /////////////////////////////////////
$actual_link = 'https://' . $_SERVER['HTTP_HOST'] . '/';
$json_url = "json.json"; $json = file_get_contents($json_url); $centre = json_decode($json, TRUE);
if (isset($centre[$actual_link])) { defined('BASE') || define('BASE', $actual_link); defined('BDD_HOST') || define('BDD_HOST', $centre[$actual_link]['db_host']); defined('BDD_DATABASE') || define('BDD_DATABASE', $centre[$actual_link]['db_database']); defined('BDD_USERNAME') || define('BDD_USERNAME', $centre[$actual_link]['db_username']); defined('BDD_PASS') || define('BDD_PASS', $centre[$actual_link]['db_password']); } else { header('Location: http://myapp.com'); exit; }
///////////////////////////////////// ////// END MULTI BUSINESS CENTER /////////////////////////////////////
And edit config/App.php and config/Database.php
PHP Code: public $default = [ 'DSN' => '', 'hostname' => BDD_HOST, 'username' => BDD_USERNAME, 'password' => BDD_PASS, 'database' => BDD_DATABASE, 'DBDriver' => 'MySQLi', 'DBPrefix' => '', 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'charset' => 'utf8', 'DBCollat' => 'utf8_general_ci', 'swapPre' => '', 'encrypt' => false, 'compress' => false, 'strictOn' => false, 'failover' => [], 'port' => 3306, ];
If the center exists, it loads the correct base url and database, otherwise it redirects to the root of the domain
|