Welcome Guest, Not a member yet? Register   Sign In
Global view variables
#1

If I'm not mistaken in CodeIgniter 4 there was a method for setting variables to be available to all views.  Is it possible to do the same in CI4? An example of what I want to achieve is:
I have a logged in user name stored in a variable. I want to have it available for all header views in every method which uses the view in Dashboard controller. I guess I could just call session() in my view and pull data from there, but that's not the best way to use views, I guess. I'd rather pull the data from session somewhere once and assign it to a globally-available-for-views variable instead.
As things stand I'm passing my variables to my header view every time the view is needed in Dashboard, which results in a lot of repeated coded in controllers.
Any help on this appreciated Wink.
Reply
#2

@forumz.

This post is the way I manage Global variables:

https://forum.codeigniter.com/thread-795...#pid387986
Reply
#3

Is there a way to pass a variable to a template once for all the routeable methods in a controller? I'm pretty sure there was in CI3 and I also had one in Kohana. Just can't find anything in CI4 documentation for Views. I guess,since templates are just views, this can be done in the controller's __construct method by passing an array of data to the template. But, is there any better way?
Reply
#4

Create the view render and then you can use setData.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB