Helpers loading with each include statement |
I have helpers that are loading site wide (menu stuff, message notifications, etc). I just noticed in the debugger that the helpers appear to be firing off on the initial page/view load and with each "include" within that view. Some helpers are database calls. This is causing the database to be hit 5-6 times PER PAGE for the exact same information. Is there a way to not have CI 4 repeatedly load a helper? The helpers are being loaded in the BaseController as they are needed on pretty much all pages (seemed a better solution that trying to remember to include in each individual controller):
Code: protected $helpers = ["form", "auth", "phone", "text","siteinfo","state","number","money"]; For example this code: PHP Code: <?= $this->include('_partials/topbar') ?> Code: SELECT COUNT(*) AS `numrows` FROM `lead_main` WHERE `status` != "Cancelled"
Loading a helper file doesn't call any of its functions. Your helper files must be wrong. It should only contain function definition and not call anything directly.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Here is the contents of one of my Helpers ("siteinfo") as an example:
PHP Code: <?php How else could I retrieve this type of information on every page? It was previously suggested that helpers were the way to go. I'm somewhat new to this and open to suggestions! It just dawned on me that those should be individual functions and each called where they are needed. My thought process was backwards on this. I was thinking I was calling this once and loading a "variable" with the information when in fact I am calling this function/helper every time I display one of the elements. Doh! Sorry for the silly question.
(08-09-2021, 03:59 PM)SoccerGuy3 Wrote: It just dawned on me that those should be individual functions and each called where they are needed. My thought process was backwards on this. I was thinking I was calling this once and loading a "variable" with the information when in fact I am calling this function/helper every time I display one of the elements. Then maybe a library would be better for this use case. You can load the info in a member variable of your class and if you call it again later, you can use the same instance of the library, so it doesn't need to get the data from the database. It would just return the data stored in memory. Of course this is only true if you need the same info in the same execution cycle.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Helpers in CodeIgniter are not really necessary anymore. Just use a class to wrap often used miscellaneous functions that don't make sense to include in your other classes. I believe helpers are more or less a holdover from previous versions of CI.
Simpler is always better
(08-10-2021, 03:14 AM)donpwinston Wrote: Helpers in CodeIgniter are not really necessary anymore. Just use a class to wrap often used miscellaneous functions that don't make sense to include in your other classes. I believe helpers are more or less a holdover from previous versions of CI. They’re still useful, but only for super simple tasks. I wouldn’t use a helper function to access the database.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
(08-10-2021, 03:14 AM)donpwinston Wrote: Helpers in CodeIgniter are not really necessary anymore. Just use a class to wrap often used miscellaneous functions that don't make sense to include in your other classes. I believe helpers are more or less a holdover from previous versions of CI. Always looking to learn. Can you give me an example of how you would do this? I want to display a badge with the count of ToDo's for the user at the top of the screen. This would be on every screen so would need to be accomplished outside the controller for that page (since there are dozens of controllers, each with dozens of pages). Needs to be something global in other words.
Thanks everyone. Ended up changing to "view cells" methodology and lowered my database query count for my dashboard from 61 queries to 11!!
|
Welcome Guest, Not a member yet? Register Sign In |