Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

php - CodeIgniter: Hooks (pre_controller) loading helpers

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 =& get_instance(); would allow me to access to loading helpers but this is not the case.

Thoughts?

 $ci =& get_instance();
 $ci->load->helper('cookie');
 // does not load
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...