Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter global variable

I am using $data in all my views $this->load->view('my_view', $data);

I have also autoload a Controller following this guide Extending Core Controller

But I want to make $data global because in views there is a sidebar which is constant for whole project and displays info fetched through db in autoloaded controller

Currently I have to manually write $data['todo'] for each and fetch info from autoloaded model.

Thank You.

like image 883
Shishant Avatar asked Mar 26 '10 22:03

Shishant


People also ask

Are PHP variables global?

Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. The PHP superglobal variables are: $GLOBALS.

How do you define a constant in ci4?

-Create a folder in project root directory or app directory. -Create a file CustomConstants. php, and define your constants in that file.

What is function in PHP CodeIgniter?

CodeIgniter uses a few functions for its operation that are globally defined, and are available to you at any point. These do not require loading any libraries or helpers. is_php ($version) Parameters: $version (string) – Version number.


3 Answers

1: Create MY_Controller in application/libraries with following:

class MY_Controller extends Controller {  
  var $data;      
  //constructor function              
}

2: Replace Controller to MY_Controller in all your controller files and load views with $this->data

class Contact extends Controller { //to.. } 

class Contact extends MY_Controller { 
 $this->load->view('contact_view', $this->data);
}

this way you can perform default functions that are applicable for whole site in MY_Controller like loading settings.

like image 162
Shishant Avatar answered Oct 05 '22 22:10

Shishant


I ran into a similar problem earlier today. I found that an easier way, rather than globals, was to use constants. You can define a constants file that will load from your index.php file:

// Include additional constants
$defines_file = 'includes/defines.php';
if (file_exists($defines_file))
{
    require_once($defines_file);
} 

Then you can add your constants to the defines.php file:

define(MY_CONSTANT,'my constant info');

This way they will be available in any file throughout the system either directly: echo MY_CONSTANT; or you can assign them to variables.

I decided this way would be easier for me as I would only have 1 location to go to when/if I needed to change the constants.

More: http://codeigniter.com/forums/viewthread/56981/#280205

like image 37
stormdrain Avatar answered Oct 06 '22 00:10

stormdrain


I used a helper function to call a global function!

eg.

function get_user($userid){
    $CI =& get_instance();

    $query = $CI->db->get_where('users', array('id' => $userid), 1, 0);
    foreach ($query->result() as $row){
        // Return a object with userdata!
        return $row;
    }
}

Now I have access to my userdata everywhere..

like image 40
pkdkk Avatar answered Oct 06 '22 00:10

pkdkk