The typical way to do this is to use get_instance()
, which returns an instance of Codeigniter (actually the current controller class).
This will work from anywhere in your CI application:
get_instance()->session->set_userdata('key', 'value');
$segments = get_instance()->uri->segment_array();
// etc.
Typically in Codeigniter, you assign it to a class variable for ease of use:
class User
{
private $CI;
function __construct()
{
$this->CI = get_instance();
}
// Example
function login()
{
$user = $this->get_user();
$this->CI->session_set_userdata('user_id', $user->id);
}
}
This does create a lot of dependency on the state of CI and the different loaded classes, rather than passing instances of them in directly to the User class via Dependency Injection, but this is the typical flow for someone developing a CI library.