CodeIgniter Forums
accessing sessions in MY_loader.php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: accessing sessions in MY_loader.php (/showthread.php?tid=21816)



accessing sessions in MY_loader.php - El Forum - 08-21-2009

[eluser]Unknown[/eluser]
I'm extending my loader so that I can add some extra stuff into the view() function for my templates. Basically I have a sidebar that's updated and changed depending on what controller you're viewing, and if you're logged in or not as well as setting variables that display private messages/logout links.

How can I access the sessions from within the loader? I have sessions in the autoload config. Here is my code

Code:
class MY_Loader extends CI_Loader
{
    function view($view, $vars = array(), $return = FALSE)
    {
        if ($this->session->userdata('islogged'))
        {
            $vars['user_data'] = 'Works';
        } else {
            $vars['user_data'] = 'Fails';
        }
        parent::view($view, $vars, $return);
    }
}


The error it returns:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: MY_Loader::$session

Filename: libraries/MY_Loader.php

Line Number: 11

Fatal error: Call to a member function userdata() on a non-object in C:\wamp\www\system\application\libraries\MY_Loader.php on line 11



accessing sessions in MY_loader.php - El Forum - 08-21-2009

[eluser]pistolPete[/eluser]
You need access to the CI superobject (see user guide)

Code:
class MY_Loader extends CI_Loader
{
    function view($view, $vars = array(), $return = FALSE)
    {
        $CI =& get_instance();
        if ($CI->session->userdata('islogged'))
        {
            $vars['user_data'] = 'Works';
        } else {
            $vars['user_data'] = 'Fails';
        }
        parent::view($view, $vars, $return);
    }
}



accessing sessions in MY_loader.php - El Forum - 08-21-2009

[eluser]Unknown[/eluser]
Aahh thank you very much. I was looking in the wrong area of the user guide.