Welcome Guest, Not a member yet? Register   Sign In
accessing sessions in MY_loader.php
#1

[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
#2

[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);
    }
}
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB