Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending the Session Library in Codeigniter 3

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?

like image 875
luv2Code Avatar asked Nov 09 '12 00:11

luv2Code


4 Answers

For the sake of completeness since 3.0 has long been released: if you want to extend the Session class, you need to add your extended class MY_Session.php to the application/libraries/Session directory to mimic its system/libraries/Session/Session.php counterpart.

After you have the correct directory structure, your MY_Session.php file should contain the usual way to extend system library classes, as @Starx has noted.

like image 105
Mirkules Avatar answered Nov 18 '22 15:11

Mirkules


Currently it is impossible to do that, but there's a pending pull request that tries to solve the problem.

However, you shouldn't post questions related to yet unreleased software. Anything that is in a development stage might be changed again, become obsolete, etc. and your question here would automatically become irrelevant to everybody, including yourself. You should post/ask your questions on the repository itself (and search for an already existing issue of course).

like image 21
Narf Avatar answered Nov 18 '22 15:11

Narf


It is possible to extend the Session class in CodeIgniter 3. Admittedly it is a bit of a kludge, but I only use this to fix a long-standing bug in the Session class anyways. Here is an example MY_Session.php library for CI3:

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

require_once SYSDIR . '/libraries/Driver.php';
require_once SYSDIR . '/libraries/Session/Session.php';

/*
    Change the following if you want to use a different driver.
*/
require_once SYSDIR . '/libraries/Session/drivers/Session_cookie.php';

class MY_Session extends CI_Session_cookie
{

    function __construct()
    {
        parent::__construct();
    }

    protected function _sess_update($force = false)
    {
        // Do NOT update an existing session on AJAX calls.
        if ($force || !$this->CI->input->is_ajax_request())
            return parent::_sess_update($force);
    }

}

/* End of file MY_Session.php */
/* Location: ./application/libraries/MY_Session.php */

For more info, or to keep up with this if it changes: http://degreesofzero.com/article/55

Disclosure: This is my personal blog.

like image 45
c.hill Avatar answered Nov 18 '22 16:11

c.hill


I managed to extend CI_Session in Codeigniter 3.1.3. Here is what I did:

Create a file application/libraries/Session/MY_Session.php

<?php                                                                                                                                                                                           
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Session extends CI_Session {

    public function __construct(array $params = array())
    {   
        if ( $this->ignore_sessions() )
            return;
        parent::__construct();
    }   

    function ignore_sessions()
    {   
        $uri = str_replace ("//", "/", $_SERVER['REQUEST_URI']);
        if ( strpos($uri, '/ignore_this_controller/') === 0 ) 
            return true;
        return false;
    }   
}  

You might also want to add 'session' to your config/autoload.php:

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

like image 39
Shemeshey Avatar answered Nov 18 '22 16:11

Shemeshey