Making data available in ALL views |
(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. 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.
(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. 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.
Another way that you can do it is by using View Cells.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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?
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 ?>
in basecontroller i do this:
$this->renderer = \Config\Services::renderer(); $this->renderer->setData(['siteinfo'=>$siteinfo]); so in views html file <?=dd($siteinfo)?>
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/codeig...Filter.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. |
Welcome Guest, Not a member yet? Register Sign In |