codeigniter news section tutorial not working

2019-07-17 04:51发布

问题:

hey guys im going thru the news section tutorial on codeigniter and i get the following error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: News::$db

Filename: core/Model.php

Line Number: 77

Backtrace:

File: /Applications/MAMP/htdocs/CodeIgniter/application/models/News_model.php
Line: 13
Function: __get

File: /Applications/MAMP/htdocs/CodeIgniter/application/controllers/News.php
Line: 10
Function: get_news

File: /Applications/MAMP/htdocs/CodeIgniter/index.php
Line: 292
Function: require_once

All i did was simply copy what was provided in the tutorial, here are my files

news_model.php file

<?php

class News_model extends CI_Model {

    public function __constrcut() {
        $this->load->database();
    }

    public function get_news($slug = FALSE) {

        if ($slug === FALSE ) {

            $query = $this->db->get('news');
            return $query->result_array();
        }


        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
    }
}

news.php

<?php 
class News extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('news_model');
    }

    public function index() {
        $data['news'] = $this->news_model->get_news();
        $data['title'] = 'News archive';

        $this->load->view('templates/header',$data);
        $this->load->view('news/index',$data);
        $this->load->view('templates/footer');
    }

    public function view($slug) {
        $data['news'] = $this->news_model->get_news($slug);

        if (empty($data['news_item'])) {
            show_404();

        }

        $data['title'] = $data['news_item']['title'];
        $this->load->view('templates/header',$data);
        $this->load->view('news/view',$data);
        $this->load->view('templates/footer');

    }

}

and here is a picture of my file structure

im trying my best to learn and debug but i dont know what it could be.

回答1:

Maybe, database library is not loaded. you have to load database library.

application / config / autoload.php

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