Making data available in ALL views |
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!
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 )
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. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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).
(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
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 |
Welcome Guest, Not a member yet? Register Sign In |