CodeIgniter Forums
Helpers loading with each include statement - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Helpers loading with each include statement (/thread-79889.html)



Helpers loading with each include statement - SoccerGuy3 - 08-09-2021

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'?>
 <?= $this->include('_partials/sidebar'?>
Results in:
Code:
SELECT COUNT(*) AS `numrows` FROM `lead_main` WHERE `status` != "Cancelled"
SELECT COUNT(*) AS `numrows` FROM `lead_main` WHERE `status` = "Future Prospect"
SELECT COUNT(*) AS `numrows` FROM `customer_main` WHERE `status_customer` != "Closed"
SELECT * FROM `todo` WHERE `assigned_to` = '1' AND `date_completed` IS NULL ORDER BY `date_due`
SELECT COUNT(*) AS `numrows` FROM `lead_main` WHERE `status` != "Cancelled"
SELECT COUNT(*) AS `numrows` FROM `lead_main` WHERE `status` = "Future Prospect"
SELECT COUNT(*) AS `numrows` FROM `customer_main` WHERE `status_customer` != "Closed"
SELECT * FROM `todo` WHERE `assigned_to` = '1' AND `date_completed` IS NULL ORDER BY `date_due`
Suggestions? Am I using the helpers incorrectly?


RE: Helpers loading with each include statement - includebeer - 08-09-2021

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.


RE: Helpers loading with each include statement - SoccerGuy3 - 08-09-2021

Here is the contents of one of my Helpers ("siteinfo") as an example:

PHP Code:
<?php

if (! function_exists('siteinfo')) {

 function 
siteinfo()
 {
 
$leadModel = new \App\Models\LeadsModel();
 
$leads $leadModel ->where('status != "Cancelled"')
 ->
countAllResults();

 
$futureleads $leadModel ->where('status = "Future Prospect"')
 ->
countAllResults();

 
$custModel = new \App\Models\CustomersModel();
 
$custs $custModel ->where('status_customer != "Closed"')
 ->
countAllResults();

 
$todoModel = new \App\Models\ToDoModel();
 
$todos $todoModel ->where('assigned_to',current_user()->id)
 ->
where('date_completed',NULL)
 ->
orderBy('date_due')
 ->
findall();

 
$servicesModel = new \App\Models\ServicesModel();
 
$r2i $servicesModel ->where('status_id',1)
 ->
countAllResults();

 
$actives $servicesModel ->where('status_id',2)
 ->
countAllResults();

 
$data = array(
 
'leads' => $leads,
 
'leads_future' => $futureleads,
 
'customers' => $custs,
 
'todos' => $todos,
 
'r2i' => $r2i,
 
'actives' => $actives,
 );

 return 
$data;
 }


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.


RE: Helpers loading with each include statement - includebeer - 08-09-2021

(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.
Doh!
Sorry for the silly question.

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.


RE: Helpers loading with each include statement - donpwinston - 08-10-2021

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.


RE: Helpers loading with each include statement - includebeer - 08-10-2021

(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.


RE: Helpers loading with each include statement - SoccerGuy3 - 08-10-2021

(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.


RE: Helpers loading with each include statement - SoccerGuy3 - 08-10-2021

Thanks everyone. Ended up changing to "view cells" methodology and lowered my database query count for my dashboard from 61 queries to 11!!