Khrisna Gunanasurya
Khrisna Gunanasurya

Reputation: 745

How to make a dynamic variable value global in CodeIgniter?

How to make a global variable that always changing?

I want to create something like protected $global_data; in controller Global_data.php that can be called by a lot of controller, and return variable $global_data value to whenever controller that has $this->load->library('../controllers/Global_data').

But when I tried to call it, it gives me this error Unable to locate the specified class: Session.php, so I think CodeIgniter 3.1.8 not allowed me to do this.

So how to achieve what I'm looking for? do I need to put it on model instead, library file or is there another way to do it?

Thank you.

Here is Global_data.php content

protected $global_data;

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

    $this->global_data = array(
        'can_be_anything'    => 'can_be_anything',
        'can_be_anything'    => 'can_be_anything',
        'can_be_anything'    => 'can_be_anything',
        'can_be_anything'    => 'can_be_anything',
    );
}

Can_be_anything_controller.php content

class Can_be_anything_controller extends CI_Controller {
    public function __construct()
    {
        parent::__construct();

        $this->load->library('../controllers/Global_data');
    }

    public function index()
    {
        $data = $this->global_data;
        $data['page_title']     = 'Dashboard';
        $data['page_directory'] = 'pages/dashboard';
        $this->load->view('template', $data);
    }
}

Upvotes: 3

Views: 2493

Answers (3)

DFriend
DFriend

Reputation: 8964

This kind of data can also be handled very nicely by config files.

/application/config/global_data.php

<php

$config['foo'] = "some foo";
$config['bar'] = 42;
$config['baz'] = array('one', 'two', 'three');

In a controller load the config file with

$this->config->load('global_data');

The access the items using

echo $this->config->item('foo');
echo $this->config->item('bar') * 2; //outputs 84
$data = $this->config->item('baz');

Config documentation

Upvotes: 2

Alex
Alex

Reputation: 9265

Make a MY_Controller in application/core/MY_Controller.php

class MY_Controller extends CI_Controller {

    public $global_data;

    public function __construct() {
        parent::__construct();
        $this->global_data = 'whateveryouwant';
    }

    public function somemethod() {
        return '123';
    }
}

then any other controllers in your application/controllers that need to access global_data should extend it like so:

class Somecontroller extends MY_Controller {

    public function index() {
         echo $this->global_data;
         echo $this->somemethod(); // works with methods too
    }

}

If you need to run more complex code just put everything into a library or model and autoload it. All public methods and properties are globally available. HMVC seems overkill for what you want.

Upvotes: 2

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

You may create a library for that in libraries directory

Global_data.php file

class Global_data{
  public $global_data;

  protected $CI;

  public function __construct() {
    $this->CI = & get_instance();
  }

  public function common_data()
  {

    $this->global_data= array(
        'can_be_anything'    => 'can_be_anything',
        'can_be_anything'    => 'can_be_anything',
        'can_be_anything'    => 'can_be_anything',
        'can_be_anything'    => 'can_be_anything',
    );
    return $this->global_data;
  }

 public function any_method(){
   $query = $this->CI->db->get('table_name');
 }
}

Now you can load it in any controller like

$this->load->library('Global_data')

Then use data

$data = $this->Global_data->common_data();

Also you may use HMVC model to use any method in any controller

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

Upvotes: 4

Related Questions