Codeigniter does not load CI super object

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

I am trying to catch session in my hook .

Here is my hook download code:

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

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

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

He continues to throw an error, which is as follows:

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

Any idea what I'm doing wrong? Does the get_instance method seem to not work correctly?

Any help would be appreciated, Thanks

Alain

+6
source share
1 answer

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

pre_controller hook Called immediately before calling any of your controllers. All base classes, routing, and security checks have been completed.

This is a CI Controller that allows you to access get_instance() . Until the controller is created, nothing will work.

Try post_controller_constructor and see if you get the desired results.

In system/Core/Controller.php :

 class CI_Controller { // <snip> public static function &get_instance() { return self::$instance; } } // END Controller class 
+8
source

Source: https://habr.com/ru/post/886325/


All Articles