Welcome Guest, Not a member yet? Register   Sign In
How to reuse an established DB connection in an another controller ?


I'm a newbie in the use of MVC frameworks. I have choosen CodeIgniter for many known reasons.
From now on, I enjoy working/playing with it. But now serious things are coming and real questions too. I have a question wich may seems basic to many of you :

I have a 'main' Controller wich via a Login Model establish a connection to an Oracle DB, after few basic controls about the user.
How can I reuse the connection establihed in the main controller in an another controller ?
Must I redo a DB connection into that controller too ? Have I missed something ?

Thank for helping me,

Not at all required. If you autload the database by editing application/config/autoload.php you dont have to keep on using
repeatedly in each controller. CI does it for you.
You just have to edit the autoload array to
$autoload['libraries'] = array('database');

Thank for your answer. I see what you mean but the autoload is Ok if you know the user/password but here the user has to log in the application and based on what the user entered as info I do this in my main controller :

$dsn = 'oci8://'.$user.':'.$password.'@local_db';        

$user and $password are not already known while the autoload runs.

Do I have to repeat that operation in every controller ?

Where does the username and password come from?

the user/password come from a login view with a form.

OK, so your user will need to be logged in, and will therefore have cookies enabled. You could store the username and password in the session, and create a custom controller that will load the database. Something like this:

class MY_Controller extends Controller {
    function MY_Controller()

        if ($db_info = this->session->userdata('db_data');)
            // The users information can be stored in an array in the session.
            $dsn = 'oci8://' . $db_info['username'] . ':' . $db_info['password'] . '@local_db';        

// End of file: MY_Controller.php
// Location: ./system/application/libraries/MY_Controller.php
The above code is untested.

Just ensure any controllers that require your user to be logged in extend the MY_Controller class.

You might need some more logic in there to handle situations where the information is incorrect before the database is called upon by any of your controller methods, but hopefully this will help you.


Yes indeed, that's the kind of thing I was looking for.

I have seen that some people load manually the database into a Model and others into a Controller.
What is the best practice ?

It all depends. I wouldn't say there's a best practice. It all depends on where you need to database. If you don't user it in your controller, and do in your model, then the model would probably be the better option, if you do use it in your controller, load it there. It's really up to you, so do what makes sense to you.

Theme © iAndrew 2016 - Forum software by © MyBB