Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variables in CodeIgniter not Working

I want to generate global variables in CodeIgniter by creating my own library and config file. This is what I wrote ini my library file, let's say globalvars.php. I put it in /application/libraries.

class Globalvars{

    function __construct($config = array()) 
    {
        foreach ($config as $key => $value) {
            $data[$key] = $value;
        }
        $CI =& get_instance();
        $CI->load->library('session');
        $CI->load->vars($data); 
    }
}

I want the user id stored in the session to be available in global variable, so I wrote this in my config file. It's named also globalvars.php. It's in /application/config directory.

$config['user']=$this->session->userdata('id');

I then test to see if it's working by write it in my controller this way.

echo $data['user'];

But I get this error in the browser

Message: Undefined property: CI_Loader::$session
Filename: config/globalvars.php

It seems that the session functions is not defined yet. How can I get it work? What have I missed here? Any help would be appreciated.

like image 228
Abaij Avatar asked Mar 06 '26 01:03

Abaij


2 Answers

You cannot use the session library in config file.

The config files are loaded before any libraries, so $this->session is undefined.

like image 53
MegaHit Avatar answered Mar 07 '26 20:03

MegaHit


The config.php has to be loaded in order for the Session class to even be initialized, as it reads settings from that file.

A lot of issues with this type of thing (setting some "global" data) can be resolved using a base controller, and extending it in your controllers.

// core/MY_Controller.php
MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct(); // Now the Session class should be loaded
        // set config items here 
    }
}

"Normal" controllers will now extend MY_Controller to take advantage of this.

See: http://codeigniter.com/user_guide/general/core_classes.html for more details.

In addition, when you load->vars(), they are available to the view layer only, it does not create a global variable called $data as you seem to be trying to access. If you do this:

$this->load->vars(array('user' => '1'));

You would access it in a file loaded by $this->load->view() like this:

echo $user;  // outputs "1"

See: http://codeigniter.com/user_guide/libraries/loader.html

$this->load->vars($array)

This function takes an associative array as input and generates variables using the PHP extract function. This function produces the same result as using the second parameter of the $this->load->view() function above. The reason you might want to use this function independently is if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. You can have multiple calls to this function. The data get cached and merged into one array for conversion to variables.

I will say that as an experienced Codeigniter user, the concept of a "global vars" class is a bit wonky and probably unnecessary, especially when it's already so easy to get and set config items. You could definitely run into some confusing issues and variable name conflicts with this method (pre-loading lots of view variables on every request).

like image 41
Wesley Murch Avatar answered Mar 07 '26 21:03

Wesley Murch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!