Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter not loading CI super object

I am trying to write a hook for my Codeigniter application.

I'm trying to catch a session in my hook.

Here is my code to load the hook:

$hook['pre_controller'] = array(
  'function' => 'getNav',
  'filename' => 'LoadNav.php',
  'filepath' => 'hooks'
);

And here is the code I'm trying to load in the hook:

function getNav()
{
     $CI =& get_instance();
     $level = $CI->session->userdata('level');
}

It keeps throwing an error which is the following:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/LoadNav.php
Line Number: 7

Any idea of what I'm doing wrong? It seems like the get_instance method is not functioning right?

Any help would be appreciated, Thanks

Alain

like image 211
criticerz Avatar asked Apr 20 '11 15:04

criticerz


1 Answers

You cannot access the $CI instance in a pre_controller hook - as per the docs:

pre_controller hook Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done..

It is the CI Controllerwhich allows access to get_instance(). Until a controller is instantiated, there is nothing to get.

Try post_controller_constructor instead and see if that gets you the desired results.

In system/Core/Controller.php:

class CI_Controller {

// <snip>

    public static function &get_instance()
    {
        return self::$instance;
    }

}
    // END Controller class
like image 119
Ross Avatar answered Nov 06 '22 05:11

Ross