Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对非对象调用成员函数get_news()_Php_Codeigniter - Fatal编程技术网

Php 对非对象调用成员函数get_news()

Php 对非对象调用成员函数get_news(),php,codeigniter,Php,Codeigniter,我正在遵循CodeIgniter的用户指南,这一部分有问题 它在index方法的第一行报告了对非对象的成员函数get_news()的调用错误。看起来好像模型加载不起作用。我已经将所有的news\u模型更改为news\u模型,以检查这是否是原因,但一切都发生了变化 两年前也有同样的问题,但这种情况下的解决方案对我不起作用,因为我对构造的调用定义正确 应用程序/控制器/News.php <?php class News extends CI_Controller { public fu

我正在遵循CodeIgniter的用户指南,这一部分有问题

它在
index
方法的第一行报告了对非对象的成员函数get_news()的调用错误。看起来好像模型加载不起作用。我已经将所有的
news\u模型
更改为
news\u模型
,以检查这是否是原因,但一切都发生了变化

两年前也有同样的问题,但这种情况下的解决方案对我不起作用,因为我对构造的调用定义正确

应用程序/控制器/News.php

<?php
class News extends CI_Controller {
    public function __contruct() {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url_helper');
    }

    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 = NULL) {
        $data['news_item'] = $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');
    }
}

我想知道这是否值得回答,但您有一个输入错误(s在
\uu construct()
中丢失,因此从未调用过)。此外,
url\u helper
只是被称为
url
,但您没有收到错误,因为它从未被调用过

<?php
class News extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url');
    }

它应该是控制器中的
\u结构
。。。您缺少一个s

放置调用
$this->load->model('news_model')就在有问题的行之前,并查看它是否真的加载。它确实有效,因此问题在于构造。但是在哪里呢?
url\u helper
不应该只是
url
?@Tpojka似乎无关紧要,只是被改变了,还是同样的错误。无论如何,教程使用了
url\u helper
尝试
$this->load->model('News\u model')而不是用作
$this->News\u model->get\u News().lawl!我有语法突出显示,没有注意到它没有突出显示,但它之后的一个是。。。现在它已经修复,我没有在
url\u helper
中遇到错误,所以也许这是正确的?@yzT很高兴它成功了。此外,将
url\u helper
更改为
url
,并从
index()
中删除对模型的调用。
<?php
class News extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->helper('url');
    }