问题
The main thing i want to solve is to check user session when somebody want to access admin panel. I've created MY_Controller class and placed it in library.
class MY_Controller extends CI_Session {
public $logged_in = FALSE;
public function __construct() {
parent::__construct();
$this->is_logged_in();
}
public function is_logged_in()
{
$logged = $this->userdata('user_id');
$this->logged_in = ($logged) ? TRUE : FALSE;
}
function sessdestroy() {
$this->session->sess_destroy();
}
}
This is my login page. I've created a button that redirects to admin panel (dashboard) just to test if the session check works.
class Login extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->load->view('view_login');
if(isset($_POST['redirect'])) {
redirect('admin');
}
}
}
This is the admin controller where i check if the user session exists.
class Admin extends MY_Controller {
function construct() {
parent::__construct();
}
function index() {
if($this->is_logged_in()) {
$this->dodadi_destinacija();
}
else redirect('login');
}
}
BUT i got this error.
Fatal error: Class 'MY_Controller' not found in C:\wamp\www\UserPanel\application\controllers\admin.php on line 3
Can you tell me your thoughts about my coding solution for the given problem please :)
回答1:
- You need to extend the
CI_Controllerclass instead ofCI_Session - You should place
MY_Controllerunderapplication/core/folder instead ofapplication/library
回答2:
Codeigniter introduced a new folder called core where the libraries that doesn't require loading lives. The original CI_Controller class is one of them, so if you want the automagick pick up your MY_Controller.php file you should place it under application/core too.
When in doubt, check the extended class's original version under system directory and mirror that under application.
(Also in your first code example, i think you meant to extend CI_Controller not CI_Session)
回答3:
I got this error on a project as I'm learning CodeIgniter. My solution was to, to use your project as an example, require_once('MY_Controller'); at the top my new Controller.
That way I didn't have to move my file to the core and I could keep it in my directory of Controllers.
来源:https://stackoverflow.com/questions/13000312/my-controller-class-not-found