Question

According to the Codeignitor docs here: http://ellislab.com/codeigniter/user-guide/general/hooks.html it states:

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

However, if I create a hook pre_controller hook with:

$hook['pre_controller'][] = array(
'class'    => 'tester',
'function' => 'test',
'filename' => 'tester.php',
'filepath' => 'models',
//'params'   => array('beer', 'wine', 'snacks')
);

and the file tester.php is:

class tester extends CI_Model
{
  public function __construct()
  {
    parent::__construct();

    $this->load->library('migration');
  }

  public function test()
  {
    echo "hi";
    exit;
  }
}

I get this error:

Fatal error: Class 'CI_Model' not found in ******.php

Why is it not loading CI_Model? If I put a require_once('system/core/Model.php'); in the hooks.php file above the pre_controller definition, I get this error:

Fatal error: Call to a member function library() on a non-object in ****.php

Since it's not actually loading the CI_Model, functions such as library() would not work. How can I force it to bootstrap the CI_Model.

The first person that says "Use post_controller_constructor" will be shot on sight as that does not answer the question. I need it to load BEFORE it runs any constructor functions from the controller classes. I need access to extend the CI_Model class from the pre_controller hook.

Était-ce utile?

La solution

The short answer is that CodeIgniter doesn't work the way you want it to. At the stage that you're trying to access a model, CodeIgniter hasn't loaded the required classes and it isn't available. Depending on exactly what you're trying to achieve, there may be another way to achieve this - without using a hook/using a later hook?

Viewing /system/core/CodeIgniter.php will show when each hook is called and when other tasks are performed; loading routing, loading global functions etc.

If you insist on using this hook, then you could add this: load_class('Model', 'core'); at the top of your model file (before you declare the class), but this would be a very dirty fix.

Make sure your class names follow the correct naming convention - tester should be Tester.


Edit: as you want to run the same code on every call (assuming every controller call), this is a possible solution:

Extend the core controller, and use this new controller as a base controller for all other controllers (as they will be sharing the same functionality). In the constructor of this new base controller, add the functionality that you want to run on every call. The code in this constructor will be called before any other code in any of your controllers.

Create the following file, application/core/MY_Controller.php.

class MY_Controller extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        // Do whatever you want - load a model, call a function...
    }
}

Extend every controller in application/controllers, with MY_Controller, rather than CI_Controller.

class Welcome extends MY_Controller {
    function __construct()
    {
        parent::__construct();
    }
    // Your controllers other functions...
}

Autres conseils

/*application/config/hooks.php*/
$hook['pre_controller'][] = 
array(
     'class'    => 'MyClass',
     'function' => 'Myfunction',
     'filename' => 'Myclass.php',
     'filepath' => 'hooks',
     'params'   => array('beer', 'wine', 'snacks')
                                );
/*application/config/config.php*/
$config['enable_hooks'] = TRUE;

/hooks/Myclass.php/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top