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';

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

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