How to load database from url

Let me explain how the application works.

- Each company account has its own separate database

- Lets assume the company name is "acme"

- The database settings (hostname, port, dbname, username, password) is specified in system/application/config/database.php file as a group called "acme"

$db['acme']['hostname'] = "localhost";
$db['acme']['username'] = "root";
$db['acme']['password'] = "root";
$db['acme']['database'] = "acmedb";

- The URL is of the format http://sitename/acme, where the "acme" should load the database group settings related to acme. Internally it should automatically load the following database settings :


- The controllers/action are normally accessible via http://sitename/acme/controller/action.


- What is the best way to do this ? (I dont mind modifying the core files if needed).

[eluser]Matt S.[/eluser]
You could extend the base controller class and add a "switch" that runs off of the URI segment in the constructor. Make a new file in:


And use this code:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller {

     function MY_Controller()



/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */

You will have to extend MY_Controller in place of Controller if you wish to use this method:

class Acme extends MY_Controller {

      function Acme()


thanks a lot ! I will think about that.

one more thing, how to handle the controllers and views url's and other functions like redirect(), anchor(), etc...

[eluser]Matt S.[/eluser]
I'm a little confused on what you're asking. Could you elaborate on what you mean by "handling" them?

If the code does redirect("group/new") it should go to http://sitename/acme/group/new and not http://sitename/group/new

Also the controller will be set as "acme" and not "group"

[eluser]Matt S.[/eluser]
You will probably have to add the segment manually into the redirect() function like this:

redirect( $this->uri->segment(1) . "/group/new");

That is assuming you are calling the redirect() function from somewhere that has "acme" in the URL.


also what will be the controller, action, parameter names that will be assigned by CI ?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.