Welcome Guest, Not a member yet? Register   Sign In
Multi domain / database
#1

(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 !
Reply
#2

so all 3 database has the same table but different data, right?
Learning Codeigniter 
Reply
#3

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($jsonTRUE);

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 $baseURL BASE

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 Smile
Reply
#4

I would say that instead of saving everything about your DB in a JSON, you can create 3 different DB groups, and based on the domain check you can define the DB group only.
Learning Codeigniter 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB