CodeIgniter Forums
Hooks - pre_controller vs. post_controller_constructor - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Hooks - pre_controller vs. post_controller_constructor (/showthread.php?tid=67695)



Hooks - pre_controller vs. post_controller_constructor - Sezu - 03-28-2017

I got two hooks. "guardian" and "orm" both of them are now starting as "post_controller_constructor" beacuse I need db connection. 
But side effect is, that I can't use Orm and Guardian methods in controller constructor because they are not loaded yet. 
And it is very anoying to write the same code in to all controller methods because I can't use constructor to put it in.

I passed through by writing method postConstructor() which is called as first in controller method. And I put repeatly used code in it. 

But I don't like this sollution. 

Does enybody have an idea how to solve this better way?


RE: Hooks - pre_controller vs. post_controller_constructor - neuron - 03-28-2017

for db connection model, view and controller u can use $CI =& get_instance(); $CI->db->....


RE: Hooks - pre_controller vs. post_controller_constructor - spjonez - 03-28-2017

If you're autoloading the DB library you should be able to use pre_controller.


RE: Hooks - pre_controller vs. post_controller_constructor - Sezu - 03-28-2017

I tried but, after I attached hooks pre_controller, there was no DB connection available.
Im not exactly sure, but CI autoloads libraries before controller constructor BUT after hooks. This seem to be reason, why there is no DB connection available in hooks.


RE: Hooks - pre_controller vs. post_controller_constructor - Narf - 03-28-2017

(03-28-2017, 08:44 AM)Sezu Wrote: Im not exactly sure, but CI autoloads libraries before controller constructor BUT after hooks.

It loads them from inside the constructor.