CustomerIgniter controller class says session is undefined

I use CodeIgniter (v1.7.2), and I created a custom controller that includes authentication called MY_Controller (based on a David Winter blog post). When I load any controllers that use this base class, I get this error:

* Message: Undefined property: MY_Controller :: $ session *

Note that I load the “session” (and “MY_controller” as a library) as follows:

$autoload['libraries'] = array('database', 'session', 'MY_Controller'); 

Here is the MY_Controller:

 class MY_Controller extends Controller { public function __construct() { parent::__construct(); if (!$this->session->userdata('loggedin')) { <-- error is here header('Location: /sessions/login'); exit(); } } } 

Here is the controller that I am trying to load:

 class Welcome extends MY_Controller { function __construct() { parent::__construct(); } function index() { $this->load->view('header'); $this->load->view('welcome_message'); $this->load->view('footer'); } } 

When I var_dump $this->session above where the error occurs, I see that it is NULL. Even creating $this->load->library('session'); in the constructor MY_Controller does not work. Why is it not loading properly?

thanks

+4
source share
1 answer

Try disabling MY_Controller from startup.

 $autoload['libraries'] = array('database', 'session'); 

You extend the controller class, which is automatically loaded using codeigniter, as it is part of the kernel

+5
source

All Articles