php codeigniter: Call to undefined method CI_Loade

2019-07-07 19:45发布

问题:

I wanted to make a webapplication using codeigniter but it is a while since I've used it and I'm getting an error whenever I try to load a model in my controller. I'm probably doing something stupid wrong but I can't figure out what it is. So please help me out if u can.

This is the error I get:

Here is the code of my model:

<?php 

class post_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getAllPosts() {
        $this->db->order_by('date', 'desc');
        $query = $this->db->get('post');
        return $query->result();
    }
}
?>

Here is the code of my controller where I load the model:

<?php 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Post extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('post_model');
    }

    public function index() {
        $data['title'] = 'Berichten';
        $data['posts'] = $this->post_model->getAllPosts();

        $this->template->load('posts', $data);
    }
}

Autoload:

$autoload['helper'] = array('url', 'form', 'date');
$autoload['model'] = array();

Solved: I couldn't solve it but I made a new project and copy-pasted my code and now it works just fine, so no idea what was wrong.

回答1:

All classes should be uppercase at the first letter

class Post_model extends CI_Model {

This throws the error when loading the model at line 11



回答2:

class post_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();//加上这句
    }

    function getAllPosts() {
        $this->db->order_by('date', 'desc');
        $query = $this->db->get('post');
        return $query->result();
    }
}


回答3:

Class name must be start with the capital letter and the file name must have the same name of that class with the extension .php Also remove the closing php tag from the model.The codeigniter class files doesn't need any close tag.It will closes automatically by the framework.



回答4:

I think your model file extension .html... change your file extention .php... correct: model_file.php incorrect: model_file.html

Check your file format.