How to load database from url

#1
[eluser]pshah.mumbai[/eluser]
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"

Code:
$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 :

Code:
$this->load->database('acme');

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

Code:
http://sitename/acme/group/new

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

#2
[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:

Code:
application/libraries/MY_Controller.php

And use this code:

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

class MY_Controller extends Controller {

     function MY_Controller()
     {
            parent::Controller();

            $this->load->database($this->uri->segment(1));
     }

}

/* 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:

Code:
class Acme extends MY_Controller {

      function Acme()
      {
          parent::MY_Controller();
      }

}

#3
[eluser]pshah.mumbai[/eluser]
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...

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

#5
[eluser]pshah.mumbai[/eluser]
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"

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

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

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

#7
[eluser]pshah.mumbai[/eluser]
Sad

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.