How am i supposed to access and change a variable over different controllers ? |
Hello , im trying to solve my mysterious problem with codeigniter !
I want to pass a value from one controller's function to another controller's function and edit it's value. This is what i have Code: class MY_Controller extends Controller { } How am i supposed to make it work ? Thanks !
Controllers handle requests by users. There is no way to share global variables across different requests.
Is this variable specific to the user? store it in a session. Does this variable pertain to the application? store it in the DB.
@invader7
Don't bloat the session. Search the Internet about what "registry pattern" is. I have got a small library for supporting such a feature. https://github.com/ivantcholakov/codeigniter-registry
@ ivantcholakov
In order for your registry library to maintain values across multiple different requests it would need be a singleton, no? Can you post a simple example of this? Perhaps something like incrementing a simple counter in the welcome/index function?
(11-19-2014, 12:07 PM)invader7 Wrote: Hello , im trying to solve my mysterious problem with codeigniter ! Although you can't pass a value from one Controller to another you can process the method in a model and return the result. Then access it from any controller you need to. If you mean to have a variable from MY_Controller and assign a value from any controller that extends MY_Controller you can do this: PHP Code: class MY_Controller extends CI_Controller { I use the $subview variable to pass the view of a controller using a layout.
I did it by saving values to database. Thank you very much for your answers !
you would use the session class to do this. something like:
Set the variable and it's value: PHP Code: $this->session->set_flashdata('test', '1234'); Get the value of the variable: PHP Code: $test = $this->session->flashdata('test'); |
Welcome Guest, Not a member yet? Register Sign In |