CodeIgniter新闻教程 - 解析错误

时间:2016-07-09 10:38:23

标签: php codeigniter

我正在关注CodeIgniter教程(http://www.codeigniter.com/user_guide/tutorial/news_section.html),当我到达“Display the news”时,不清楚在哪里添加第二个代码块:

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

我能解释本教程的唯一方法是将它添加到News.php控制器中。

<?php
class News extends CI_Controller 
{

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

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

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

当我在application / controllers / News.php的末尾添加它时,我得到一个解析错误:

Parse error: parse error in /server/application/controllers/News.php on line 33
A PHP Error was encountered

Severity: Parsing Error

Message: parse error

Filename: controllers/News.php

Line Number: 33

Backtrace:

如果我将它包含在News类中,我会得到两个PHP错误&amp;一个致命的错误:

Fatal error: Cannot redeclare News::index() in /server/application/controllers/News.php on line 32
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /server/application/controllers/News.php:32)

Filename: core/Common.php

Line Number: 573

Backtrace:

A PHP Error was encountered

Severity: Compile Error

Message: Cannot redeclare News::index()

Filename: controllers/News.php

Line Number: 32

Backtrace:

不确定还有什么要尝试?

提前致谢。

1 个答案:

答案 0 :(得分:1)

第二个代码块function get_news()应该放在News_model.php文件中。这就是你的最终控制器&amp;模型看起来像:

型号:http://screencast.com/t/XQtJpEnSC

控制器:http://screencast.com/t/Kmec1vGh