How am i supposed to access and change a variable over different controllers ? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: How am i supposed to access and change a variable over different controllers ? (/showthread.php?tid=300) |
How am i supposed to access and change a variable over different controllers ? - invader7 - 11-19-2014 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 ! RE: How am i supposed to access and change a variable over different controllers ? - bclinton - 11-19-2014 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. RE: How am i supposed to access and change a variable over different controllers ? - ivantcholakov - 11-19-2014 @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 RE: How am i supposed to access and change a variable over different controllers ? - bclinton - 11-20-2014 @ 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? RE: How am i supposed to access and change a variable over different controllers ? - ivantcholakov - 11-20-2014 @bclinton Registry is like a collection of global variables. It does not transport values between requests, it is what session class does. RE: How am i supposed to access and change a variable over different controllers ? - efenollal - 11-20-2014 (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. RE: How am i supposed to access and change a variable over different controllers ? - invader7 - 11-20-2014 I did it by saving values to database. Thank you very much for your answers ! RE: How am i supposed to access and change a variable over different controllers ? - Hobbes - 11-20-2014 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'); |