November 9, 2012

Extending the Session Library in Codeigniter 3

Question by luv2Code

I’ve been using CI for a while now and recently upgraded to CI 3. I noticed the Session library has now been moved to a folder.
I used to have my own MY_Session.php file in the application/libraries folder that extended the default CI library.

I also use the autoload.php file to autoload my session library.
This no longer works, as I get Unable to load the requested class: Session.

If I remove MY_Session.php file, then the pages load, but then I’ll be missing my extended functionality.

Does anyone know how exactly to extend the session library in CI 3?

Answer by Starx

You can do this similar to extending other core components.

class MY_Session extends CI_Session {

    function __construct() 
    {
        parent::__construct();
        echo "In extended the session";
    }
}  

Make sure you load the session library as well. Like

$autoload['libraries'] = array('database','session');

Also, Unable to load the requested class: Session are generally triggered for the two reasons.

  • CI can’t fine the session
  • You haven’t autoloaded the library

Also, make sure you have a encryption key on your config.php

$config['encryption_key'] = 'xxxxxxxxxxxxxxxxxxxxxxx';
...

Please fill the form - I will response as fast as I can!