• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"post controller" Hook Problem

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

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

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

#4
[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();


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.