Welcome Guest, Not a member yet? Register   Sign In
Can't get the CI superobject.
#1

[eluser]whizzer[/eluser]
I have enabled the hooks in my config file. Here is the contents of config/hooks:
Code:
$hook['pre_controller'] = array(
    'class'    => 'Pre_Checks',
    'function' => 'subdomain_check',
    'filename' => 'pre_checks.php',
    'filepath' => 'hooks'
    );
I this is what I have in hooks/pre_checks.php:
Code:
class Pre_Checks {
    function subdomain_check()
    {
        //get the instance of the CI superobject
        $CI =& get_instance();

        var_dump($CI);
    }

}
I get "NULL" and I can't use the methods and properties of my CI superobject (I also tried $this->CI =& get_instance() but it doesn't word either).
Thank you.
#2

[eluser]Jelmer[/eluser]
The base of CI is only instantiated when the controller is (as the controller is a grandchild of CI_Base). So calling get_instance() pre_controller will return NULL as there's no superobject yet to be retrieved.

If you need CI libs you can use load_class() to use classes. In CI2 you might need to add 'core' as the second parameter if it's a core class you're using.

Examples:
Code:
load_class('URI')->segment(4);
load_class('Input')->post('varname');

//CI2 if the above would throw an error because the class can be found and it's a core class:
load_class('Output', 'core')->set_header(...);
#3

[eluser]whizzer[/eluser]
Ok, but how can I load the database class since there isn't a database.php file in Libraries?
#4

[eluser]treeface[/eluser]
Hi whizzer,

You won't be able to access the CI superobject until after the controller constructor is run. Fortunately, you can catch the code *right after* this point by changing your hook declaration to this:

Code:
$hook['post_controller_constructor'] = array(
    'class'    => 'Pre_Checks',
    'function' => 'subdomain_check',
    'filename' => 'pre_checks.php',
    'filepath' => 'hooks'
    );

Cheers




Theme © iAndrew 2016 - Forum software by © MyBB