Leveraging "load->_ci_cached_vars" array in libraries |
[eluser]neal789[/eluser]
Just wondering if anyone sees any downside to leveraging the _ci_cached_vars method of the CI_Loader class as a substitute for passing variables from my controllers into library functions? It seems like I could clean up my code a lot and not need to worry about passing in session information to libraries if I just access the load->_ci_cached_vars array within my libraries to get user information. Can I depend on that array being correct? Is there any downside or risk? Thanks for any input.
[eluser]Pascal Kriete[/eluser]
Could you give us an example? Why don't you just make a "settings" library, or use the session or config lib? _ci_cached_vars is a strange choice, it's just going to litter your view scope with variables.
[eluser]neal789[/eluser]
Thanks for the reply Pascal Kriete. I don't have a specific code example to give, but for all logged in users on the site I grab a rather large "profile" object at the start of each controller. I want it to be available to me within that request no matter what library or model I'm in. And I want to do it without constantly passing the object around. The profile object is too large to cookie, so adding it to the session cookie is not an option. I could create a settings library, but I think that the config library might be the way to go. I didn't realize that I could add to that dynamically. I'm afraid I don't understand the term "view scope" so I didn't understand your last comment, but let me know if this shed any more light on what I'm talking about and if you think I'm on the right track. Thanks
[eluser]Pascal Kriete[/eluser]
_ci_cached_vars is used to store the current view variables. So both of these examples add to _ci_cached_vars Code: $data = array('foo' => 'bar'); That array is then extracted when the view is loaded. Which means that in the view, you have access to the variables in _ci_cached_vars. It sounds like your on the right track. I personally like keeping that kind of stuff separate from CI's session and config data arrays. So I would probably extend the session or config library to add a nice getter for profile data. Code: $user_language = $this->session->user_profile('language', 'english'); Something like: Code: class MY_Session extends CI_Session {
[eluser]neal789[/eluser]
That looks like a very elegant solution. I'm going to take your advice and run with it. Thanks so much for your input. Neal |
Welcome Guest, Not a member yet? Register Sign In |