Welcome Guest, Not a member yet? Register   Sign In
Where to put a "global variable" for the entire website
#6

[eluser]simshaun[/eluser]
What if you need a global variable whose value comes from a model?
Wherever the variable is set, I need to be able to pass $CI->session->userdata('user_id') to the model's method.

Example:
$cur_user = $CI->UsersModel->get_user_data($CI->session->userdata('user_id'));

I've tried from the config, but its not letting me perform $CI->session->userdata('user_id'). (I'm assuming because the session lib hasn't been loaded at that point, but I don't know.)

Edit:
I found a temporary solution.

I've created this library which is autoloaded.
It loads the UsersModel and creates a key in the config just like I want.
Now I can call $this->config->item('cur_user_level') in my controllers.
Is this effective? If not, how should I do it?

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Class is auto-loaded by CI.
*/
class AuthInit {

    var $CI;

    function AuthInit()
    {
        $this->init();
    }

    /**
    * Selectively set config items that contain the logged in user's data.
    */
    function init()
    {
        $this->_get_ci();
        $this->CI->load->model('UsersModel');

        $user_level    = $this->CI->UsersModel->get_user_level($this->CI->session->userdata('user_id'));

        $this->CI->config->set_item('cur_user_level', $user_level);
    }

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

// end of file


Messages In This Thread
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 08:08 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:21 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:24 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:28 PM
Where to put a "global variable" for the entire website - by El Forum - 11-26-2008, 09:39 PM
Where to put a "global variable" for the entire website - by El Forum - 12-29-2008, 07:37 PM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 02:31 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 02:51 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 03:07 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 04:32 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 05:04 AM
Where to put a "global variable" for the entire website - by El Forum - 12-30-2008, 09:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB