How Autoload function on base controller ? - 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 Autoload function on base controller ? (/showthread.php?tid=77613) |
How Autoload function on base controller ? - iblisious - 09-24-2020 I try to migrate from CI3 to CI4 and my problem is; in CI3 I can call helper function inside MY_Controller but in CI4 it doesn't work. in MY_Controller.php PHP Code: <?php in my sideMenu Libraries PHP Code: <?php in my menu helper PHP Code: <?php How can I call my helper function inside BaseController ? Because if I use the code above and try to making adjustments to CI4 construction then calling $sidebar var from view, is just show nothing. Thank you before. RE: How Autoload function on base controller ? - captain-sensible - 09-24-2020 Re: helper function in CI 4.0.4 what i'm currently doing in BaseController.php Code: <?php then in my controller Code: <?php namespace App\Controllers; because all my controllers "extend" BaseController they get functionality of helper. So for instance on view i want footer to display Copywrite from : to I pass date to view in controller's as: Code: 'date'=>$this->myDate does this give you any clue ? i'm sure somebody will tell me there's a more elegant way of doing it but main points of my point are: 1) if you declare helper in BaseController and then extend your Controller from BaseController that saves you to having to repeat helper code in every controller you use. I use a construct in BaseController so that when i use a controller that will be called. I don't call "$this->myDate" in each controller because its a member property of the BAseController but because my controllers extend BAseController they inherit or have access to attribute of BaseController. I probably need to review public, protected at some point but my approach is :get ot working" then refine later RE: How Autoload function on base controller ? - iblisious - 09-27-2020 (09-24-2020, 07:49 AM)captain-sensible Wrote: Re: helper function the helper function called first and in the var $data['sidebar'] I called the sidebar menu.. If i try to use my code into CI4, seems like the helper function doesn't works, it's make the variable $data['sidebar'] return nothing.. RE: How Autoload function on base controller ? - InsiteFX - 09-27-2020 You can also load helpers in the app/Common.php RE: How Autoload function on base controller ? - iblisious - 09-27-2020 (09-27-2020, 03:54 PM)InsiteFX Wrote: You can also load helpers in the app/Common.php I try to using my helper function inside Common.php and called inside BaseController.php but still not work.. When I call variable sidebar, still return nothing.. I just wondering, why it's doesn't work on CI4 ? because I try the code above in a new CI3 and it's work. RE: How Autoload function on base controller ? - InsiteFX - 09-27-2020 Your not calling a helper you need to autoload your Library in Config/Autoload in the class map at the bottom. Also there is no more ci instance like ci 3. For load vars I just call a dummy view first and pass all variables in to that. |