CodeIgniter Forums
How to call helper function from Controller's Constructor? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to call helper function from Controller's Constructor? (/showthread.php?tid=77956)



How to call helper function from Controller's Constructor? - munggaran - 11-10-2020

Anyone know how to call helper function from Controller's Constructor?, iIve tried and thrown an error, tried adding $this->thefunction() but it also produce an error since this will make complication with function inside controller. Currently I just put everything inside among functions instead constructor but hope there's a way to solve this  Huh


RE: How to call helper function from Controller's Constructor? - T.O.M. - 11-11-2020

If you loaded your helper then you can call it's function only with name - thefunction();


RE: How to call helper function from Controller's Constructor? - munggaran - 11-11-2020

(11-11-2020, 02:01 AM)T.O.M. Wrote: If you loaded your helper then you can call it's function only with name - thefunction();
I loaded helper in basecontroller so all controller will have the helper, it works whenever I call thefunction() inside index() or other_function(), but if I try to call from __constructor it will raise an error  Sad


RE: How to call helper function from Controller's Constructor? - T.O.M. - 11-11-2020

In BaseController constructor is initController() method (not __constructor()).
So your __constructor() is called earlier then BaseController::initController() and helpers are not loaded yet.
You can try to load helper manually in your constructor right before call its method with
PHP Code:
helper('your_helper'); 



RE: How to call helper function from Controller's Constructor? - munggaran - 11-11-2020

(11-11-2020, 06:58 AM)T.O.M. Wrote: In BaseController constructor is initController() method (not __constructor()).
So your __constructor() is called earlier then BaseController::initController() and helpers are not loaded yet.
You can try to load helper manually in your constructor right before call its method with
PHP Code:
helper('your_helper'); 


ahhh, now it's working Smile thanks mate!