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
|
Making data available in ALL views - SoccerGuy3 - 06-14-2021 I am attempting to make some data available in all view files. This information is displayed in the page masthead and sidebar menu. I know I could pull the data in each and every single controller, but that is tedious and really not efficient. I would like to pull it at some global point and have it available in the view. The key is I don't want to have to modify all the controllers to pass the data to the view (or remember to pass in the future). How can I set a variable that is available in a view in say the base controller? What I have so far (in the base controller): PHP Code: class BaseController extends Controller PHP Code: <?= $siteInfo ?> I have confirmed that $siteInfo has data before you get to the view, but in the view I get: "Undefined variable $siteInfo" Feels like I am missing something simple here... help! RE: Making data available in ALL views - paulkd - 06-14-2021 Can you not use PHP Code: <?= siteinfo(); ?> RE: Making data available in ALL views - ikesela - 06-15-2021 why don't make it as Helper (put file in helper folder, procedural function) . accessible on all view. wonder why you using so many controller , should be one controller is enough per site ( i mean: frontpage 1 controller, members 1 controller, admin 1 controller ) RE: Making data available in ALL views - InsiteFX - 06-15-2021 In your BaseController just load the helper through the helpers array Then in your view just do this. PHP Code: <?= siteinfo();?> Make sure that your helper is named siteinfo_helper. RE: Making data available in ALL views - SoccerGuy3 - 06-15-2021 (06-14-2021, 10:02 PM)paulkd Wrote: Can you not use No, when I do I get: Code: Undefined variable $siteInfo RE: Making data available in ALL views - SoccerGuy3 - 06-15-2021 (06-15-2021, 12:25 AM)ikesela Wrote: why don't make it as Helper (put file in helper folder, procedural function) . accessible on all view. wonder why you using so many controller , should be one controller is enough per site ( i mean: frontpage 1 controller, members 1 controller, admin 1 controller ) It is in a helper. My issue is I cannot access it from the view. I get "Undefined variable" when I do. This is a very large business management site. Each section of the admin has it's own controller because it is so large. For instance the customer management controller is over 600 lines alone (and I am not done with that section yet - will probably double). RE: Making data available in ALL views - SoccerGuy3 - 06-15-2021 (06-15-2021, 01:21 AM)InsiteFX Wrote: In your BaseController just load the helper through the helpers array Ok, so I changed the view code to what you said and then I removed the code from the Base Controller that calls the helper and moved it up into the array like so: Code: protected $helpers = ["form", "auth", "phone", "text","siteinfo"]; Code: Undefined variable $siteInfo RE: Making data available in ALL views - SoccerGuy3 - 06-15-2021 (06-14-2021, 10:02 PM)paulkd Wrote: Can you not use I missed that you dropped the "$"... now it works!!!!! Thanks!! RE: Making data available in ALL views - SoccerGuy3 - 06-15-2021 (06-15-2021, 01:21 AM)InsiteFX Wrote: In your BaseController just load the helper through the helpers array I missed that you dropped the "$"... now it works!!!!! Thanks!! RE: Making data available in ALL views - SoccerGuy3 - 06-15-2021 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 |