Multiple databases with multiple applications

Asked

Viewed 359 times

4

I made an application with Codeigniter which uses only a database. However, I was asked to create several copies of this application that 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 file PHP, Soon I’ll have to do it in every application. And if there are hundreds of folders, there is some way to update this file in all applications automatically?

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

  • 1

    Dear, your question is quite broad and probably someone will vote to close it by the way it is written. What I can recommend is reading about code versioning, deployment strategies and environment control for your application.

  • My dear, I realize what you said about closing. On your recommendation, I know part of what you said. What I wanted was something directly related to Codeigniter. With pure PHP it’s much simpler and I’ve done it many times. The problem is that Codeigniter for me is a bit confusing and I would like someone expert in it to give me some hint about it in Codeigniter. But thank you for the recommendation. All the tips I found on the internet are very time consuming to apply. I believe someone has some better idea and that someone might be here at Stackoverflow. Thanks friend. ;)

1 answer

3


From what I understand, you will have to use many databases, and you do not want to duplicate the application just for that. In case you were going to do it:

  1. www.meusite.com/app1
  2. www.meusite.com/app2
  3. www.meusite.com/app3
  4. www.meusite.com/app4

There is an interesting solution on the site: http://www.oficinadanet.com.br/post/10349-codeigniter-conectando-ao-banco-de-dados-automaticamente-de-acordo-com-o-ambiente

I don’t know if it’s appropriate because I’m not an expert on Codeigniter.

I’ve tested it and it works well.

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

I hope it helps!

UPDATING:

I don’t know if you’re okay with security. If anyone can help, I’d appreciate it!

Look for the file "index php." which 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 name of the folder that appears in the URL. Example: www.meusite.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!!!');
}

}

  • Thank you friend! It was really something like this I was looking for. I will test!!!

  • 1

    This way you won’t need to make copies of applications if they’re on the same domain. You only need to create special folders as in the case of storing photos and things that belong to a particular database. ;)

  • So what I saw from this solution (link) is that it checks the environment ENVIRONMENT that is to define the access to which bank and does not check the folder itself that is correct? But I imagine it was more the idea I wanted to give, and with that you have to adjust the idea to read the folder and make the relationship with the desired basis.

  • Marcelo Diniz, I did an update. If anyone sees security issues or likely future release incompatibilities, please let us know to improve the code. Thanks a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.