1

I am using codeigniter to develop my web based application. This will be used by various different clients and each will have there own database but same codebase.

I want to be able to set the name of database in database.php based on the clients who access it i.e based on the url. So if clientA.com access its then it should set the database name to clintA and if clientB.com access it then it should set it to clientB.com.

Any idea as how should I approch this.

1 Answer 1

4

There are several possible ways, but probably the easiest is to use database groups in your config file. In your application/config/database.php file use a multi-dimensional array to create different groups for each of your clients. So you could have:

$db['clientA']['hostname'] = ...
$db['clientA']['username'] = ...
etc...
$db['clientB']['hostname'] = ...
$db['clientB']['username'] = ...
etc...

Now in your application you can just pass the desired group to your load command:

$this->load->database('clientA');

Now combine that with getenv to grab the host name and you should be all set:

$clientHost = getenv('HTTP_HOST');
$this->load->database($clientHost);

Of course there could be more logic needed, but that should get you started.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks David. I will work on the line you suggested and see how it goes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.