简体   繁体   中英

php codeigniter: Call to undefined method CI_Loader::model()

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.

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

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();
    }
}

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

Check your file format.

类名必须以大写字母开头,文件名必须与该类的文件名具有扩展名.php并从模型中删除关闭的php标签。codeigniter类文件不需要任何关闭标签。框架自动关闭。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM