CodeIgniter Forums
Making data available in ALL views - 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: Making data available in ALL views (/showthread.php?tid=79431)

Pages: 1 2


RE: Making data available in ALL views - paulkd - 06-15-2021

(06-15-2021, 07:18 AM)SoccerGuy3 Wrote: Thanks everyone for the help. Just needed a push in the right direction. Somewhat new to CI and the community support is great.
Now that it is working/returning data, I wonder if there is a better way to return the data. In my view file I am using:
Code:
<?= siteInfo()['leads']; ?>

In my helper file:
Code:
<?php

if (! function_exists('siteinfo')) {

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

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

$data = array(
'leads' => $leads,
'customers' => $custs,
);

return $data;
}
}
Any suggestions to make that more efficient or easier to access? Always looking to learn new ways!

Personally, I wouldn't use a helper for what you are doing. I would have the model/database instantiation in a controller that will display the view (or possibly a base controller). The $data would be passed into the view as normal.


RE: Making data available in ALL views - SoccerGuy3 - 06-15-2021

(06-15-2021, 12:48 PM)paulkd Wrote:
(06-15-2021, 07:18 AM)SoccerGuy3 Wrote: Thanks everyone for the help. Just needed a push in the right direction. Somewhat new to CI and the community support is great.
Now that it is working/returning data, I wonder if there is a better way to return the data. In my view file I am using:
Code:
<?= siteInfo()['leads']; ?>

In my helper file:
Code:
<?php

if (! function_exists('siteinfo')) {

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

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

$data = array(
'leads' => $leads,
'customers' => $custs,
);

return $data;
}
}
Any suggestions to make that more efficient or easier to access? Always looking to learn new ways!

Personally, I wouldn't use a helper for what you are doing. I would have the model/database instantiation in a controller that will display the view (or possibly a base controller). The $data would be passed into the view as normal.

As stated back at the beginning, the helper made the most sense as I need it on every page and trying to put it into every controller just is a waste and hard to keep track of. I need the data available on every page load. Open to suggestions, but having to put it into every controller (every function?) is just not realistic for me.


RE: Making data available in ALL views - InsiteFX - 06-15-2021

Another way that you can do it is by using View Cells.


RE: Making data available in ALL views - SoccerGuy3 - 06-16-2021

(06-15-2021, 08:53 PM)InsiteFX Wrote: Another way that you can do it is by using View Cells.

That's interesting. The docs are a little sparse, but if I understand correctly. I could create a new controller (or use the Base Controller) and put a function in it to pull a piece of data and return the rendered html directly. Can I return the HTML directly or do I have to create a view that will then be included in the original HTML?


RE: Making data available in ALL views - najdanovicivan - 06-16-2021

You can create class for such use can also be and Entity and then add a service which will return it as singleton. Much better solution that having it as helper method as the loading of data will happen only once. Then you can use it in view like this



PHP Code:
<?= service('siteinfo')->leads ?>



RE: Making data available in ALL views - wuuyun - 06-16-2021

in basecontroller i do this:

$this->renderer = \Config\Services::renderer();
$this->renderer->setData(['siteinfo'=>$siteinfo]);

so in views html file

<?=dd($siteinfo)?>


RE: Making data available in ALL views - MGatner - 06-17-2021

Lots of options! I like many of the suggestions here. I’ll add one that hasn’t been brought up: Filters. I’ve been into injecting content that is used on many pages with an “after” Filter. Check out this example for building and inserting a custom menu on every page: https://github.com/tattersoftware/codeigniter4-menus/blob/develop/src/Filters/MenusFilter.php

In your case if you always knew the place where it was going you could spend it to some div instead of using the placeholder text.