I am using bonfire for my project.
I have to fetch userid from the database according to username and use that userid in every view.
Right now what I am doing is I have a model function and that function is called in every controller function to fetch that userid and then userid is set to view page.
I have to repeat that piece of code for every controller function,
My query is that Is there a way to just a userid once in the controller and use that userid for every view.
my controller function :-
class asd extends Admin_Controller {
protected $role;
public function _construct(){
parent::_construct();
$this->load->library('users/auth');
$this->load->model('helpdesk_model');
$role = $this->helpdesk_model->getRole($this->auth->username());
}
}
I am using Template::set('role',$role); in my controller function
my view :-
<?php echo Template::get('role); ?>
Its showing undefined variable role 🙁
My model :-
function getRole($username) {
$this->db->select('role_id');
$this->db->where('username',$username);
return $this->db->get('tbl_users')->row();
}
Add a variable to the controller and initialize it in the constructor:
In the view you can access it like this: