CodeIgniter: Hooks (pre_controller) loading helpers

后端 未结 2 945
温柔的废话
温柔的废话 2020-12-17 19:09

I am trying to load the cookie helper in my pre_controller hook for a \'remember me\' function on our site. I thought that creating an instance of the CI object with $ci =&a

相关标签:
2条回答
  • 2020-12-17 19:19

    The problem with the post_controller_constructor is it runs after the constructor (funnily enough) and if you use Controller constructors for lots of things this can be a problem.

    If it's not a problem for you (your helper wont affect anything run or loaded in your constructors) fair enough, if it IS a problem you have two solutions.

    1. Instead of the hook put your code in MY_Controller
    2. Create MY_Controller and add in a custom hook point.

      class MY_Controller extends Controller
      {
      
          function MY_Controller()
          {
              parent::Controller();
              $GLOBALS['EXT']->_call_hook('pre_controller_constructor');
          } 
      }
      

    Note that if you're using CodeIgniter 3.0 or later, the function _call_hook was renamed to call_hook.

    0 讨论(0)
  • 2020-12-17 19:28

    The pre_controller hook executes before the super object has been fully constructed, so get_instance() can't work - the static object it returns a reference to hasn't yet been initialized.

    Consider using the post_controller_constructor hook instead; your controller's constructor will have executed, and the CI super object will be available for use.

    0 讨论(0)
提交回复
热议问题