Multiple databases with multiple applications

4

I made an application with CodeIgniter that uses only one database. However, I was asked to create multiple copies of this application which will also access several different databases. That is, I will have dozens of applications and dozens of different databases. If I make any changes to a PHP file, I'll soon have to do it in all applications. And if there are hundreds of folders, is there any way to update this file in all applications automatically?

Otherwise I will have to update one by one which makes it complicated and time-consuming. What I really wanted was to use only one application and have it fetch the requested database. Having to write all the code using $this->db1 , $this->db2 ... is very complicated because the application is made and there are dozens of "models"!

    
asked by anonymous 04.12.2014 / 02:26

1 answer

3

From what I understand, you're going to have to use many databases, and you do not want to duplicate the application just for that. In case you would do this:

  • www.meusite.com/app1
  • www.meusite.com/app2
  • www.meusite.com/app3
  • www.meusite.com/app4
  • There is an interesting solution on the site: link

    I do not know if it's adequate because I'm not an expert on Codeigniter.

    I've tested it and it works fine.

    What I do is to know in which folder the user entered to direct to the correct database using SWITCH information.

    I hope it helps!

    UPDATE:

    I do not know if it's good for security. If anyone could help, I would appreciate it!

    Look for the index.php file that is at the root of Codeigniter and change the following lines (make a copy of the original file).

    Change "app1, app2, app3, app4" to the folder name that appears in the URL. Example: www.mysite.com/minhapasta

    //Changed constant to my folder -> request_uri
    $get_uri =  $_SERVER['REQUEST_URI'];
    
    $get_uri = filter_var($get_uri, FILTER_SANITIZE_STRING);
    
    $get_uri = explode('/',$get_uri);
    
    $get_uri = $get_uri[1];
    
    $redirect = $get_uri;
    
    define('ENVIRONMENT', $redirect);
    
    if (defined('ENVIRONMENT')) {
    switch (ENVIRONMENT) {
        case 'development':
            $db['default']['hostname'] = 'host do banco de dados';
            $db['default']['username'] = 'usuário do banco de dados';
            $db['default']['password'] = 'senha do banco de dados';
            $db['default']['database'] = 'nome do banco de dados';
            break;
    
        case 'app1':
            $db['default']['hostname'] = 'host do banco de dados';
            $db['default']['username'] = 'usuário do banco de dados';
            $db['default']['password'] = 'senha do banco de dados';
            $db['default']['database'] = 'nome do banco de dados';
            break;
    
         case 'app2':
            $db['default']['hostname'] = 'host do banco de dados';
            $db['default']['username'] = 'usuário do banco de dados';
            $db['default']['password'] = 'senha do banco de dados';
            $db['default']['database'] = 'nome do banco de dados';
            break; 
    
          case 'app3':
            $db['default']['hostname'] = 'host do banco de dados';
            $db['default']['username'] = 'usuário do banco de dados';
            $db['default']['password'] = 'senha do banco de dados';
            $db['default']['database'] = 'nome do banco de dados';
            break;  
    
          case 'app4':
            $db['default']['hostname'] = 'host do banco de dados';
            $db['default']['username'] = 'usuário do banco de dados';
            $db['default']['password'] = 'senha do banco de dados';
            $db['default']['database'] = 'nome do banco de dados';
            break;    
    
    
        case 'production':
            $db['default']['hostname'] = 'host do banco de dados';
            $db['default']['username'] = 'usuário do banco de dados';
            $db['default']['password'] = 'senha do banco de dados';
            $db['default']['database'] = 'nome do banco de dados';
            break;
    
       default:
            exit('Minha mensagem de erro personalizada aqui!!!');
    }
    

    }

        
    04.12.2014 / 16:47