I would like to know how I can add the following code to my codeigniter constructor:
$this->load->model('members_model');
$member = $this->session->userdata('email_address');
$viewdata['pagecontent'] = $this->members_model->get_profile($member);
The code is used throughout my controller and it seems silly to repeat it every time. When I try adding it to the constructor I am unable to reference the set variables.
This is the constructor so far:
public function __construct(){
parent::__construct();
Accesscontrol_helper::is_logged_in_super_user();
$this->load->model('members_model');
$member = $this->session->userdata('email_address');
$viewdata['pagecontent'] = $this->members_model->get_profile($member);
}
Why wouldn’t the above code work? Do constructors require different code?
Constructor works, in this sense, like all other methods (and like functions), so your vars are subject to the variable scope.
Do:
Then, in your other controller’s methods, you just call the class property $this->viewdata, ex.
And access it, in myview.php :