CodeIgniter Forums
"post controller" Hook Problem - 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: "post controller" Hook Problem (/showthread.php?tid=19198)



"post controller" Hook Problem - El Forum - 05-31-2009

[eluser]Unknown[/eluser]
Hi.

I can't get the instance in the post controller hook. Here is my Sourcecode.

Code:
// config/autoload.php
$autoload['libraries'][] = 'MY_Controller';

// config/hooks.php
$hook['post_controller'][] = array(
    'class'    => 'MY_Controller',
    'function' => 'test',
    'filename' => 'MY_Controller.php',
    'filepath' => 'libraries'
);

// controllers/welcome.php
class Welcome extends MY_Controller {

    function Welcome() {
        parent::MY_Controller();    
    }
    
    function index() {        
        $this->view['var1'] = 'var1';
    }
}


// libraries/MY_Controller.php
class MY_Controller extends Controller {
    
    var $view=array();

    function MY_Controller() {
        parent::Controller();
    }
    
    function test() {        
        $this->CI =& get_instance();
        print_r($this->CI->view);
    }
}

// OUTPUT
Array ( )
If i try to use $this->view the output is empty too.
Code:
// libraries/MY_Controller.php
    function test() {        
        print_r($this->view);
    }

// OUTPUT
Array ( )


If i use global $CI the output is right.
Code:
// libraries/MY_Controller.php
    function test() {        
        global $CI;
        $this->CI = $CI;
        print_r($this->CI->view);
    }

// OUTPUT
Array ( [var1] => var1 )

What's the problem? How can i get rid of the "global $CI"? Can anyone help?

Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i PHP/5.2.9

I tested the same on my Linux Server with Apache 2.2 and PHP4 and there is the get_instance version working...

Greetings,
Wolfgang


"post controller" Hook Problem - El Forum - 05-31-2009

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

I'm no expert on hooks, but you should not need to obtain a reference to the CodeIgniter Super Object, as you are already within that scope.


"post controller" Hook Problem - El Forum - 06-14-2009

[eluser]Unknown[/eluser]
Can no one help?


"post controller" Hook Problem - El Forum - 06-14-2009

[eluser]wiredesignz[/eluser]
Don't even try to call the controller in a post controller hook.

$CI is a global variable that is used to hold the controller object and is available until the CodeIgniter script finishes.

You should perhaps define a custom class or function in the hooks directory and by using global $CI you will then get access to your test method.
Code:
global $CI;
$CI->test();