CodeIgniter Forums
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/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: How am i supposed to access and change a variable over different controllers ? (/thread-300.html)



How am i supposed to access and change a variable over different controllers ? - invader7 - 11-19-2014

Hello , im trying to solve my mysterious Sad 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 { }
class MY_User extends MY_Controller {}

and

class Liveshow extends MY_Controller {
    function a() {
        //set a global $test = 123;
    }
}

class Channels extends MY_User {
    function b() {
        //echo the global variable $test
    }
}


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  Sad  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 { }
class MY_User extends MY_Controller {}

and

class Liveshow extends MY_Controller {
    function a() {
        //set a global $test = 123;
    }
}

class Channels extends MY_User {
    function b() {
        //echo the global variable $test
    }
}


How am i supposed to make it work ?  

Thanks !



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 {

 
 public $data = array();

 
   function __construct()
 
   {
 
     parent::__construct();
 
   }

 
 public function load_view($subview$your_variable'' // could be an array here)
 
 {
 
     // $this->data allows to pass any array value to the view
 
     // like errors or values.
 
     $this->data['your_variable_here'] = $your_variable;

 
     $this->data['subview'] = $subview;
 
     $this->load->view('layouts/layout'$this->data);
 
 }


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');