【问题标题】:Codeigniter news tutorial view method not workingCodeigniter 新闻教程视图方法不起作用
【发布时间】:2015-11-19 16:36:58
【问题描述】:

大家好,我正在学习 codeigniter,我正在阅读新闻教程。我几乎完成了,但我的视图方法显示 404 而不是新闻本身。我尝试使用以下代码进行调试

    echo '<pre>';
    var_dump($this->news_model->get_news($slug));
    exit();

然后返回

NULL

这就是我的控制器是如何工作的,那就是调用该方法

<?php 
class News extends CI_Controller {

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

    public function index() {

        //echo '<pre>';
        //var_dump($this->news_model->get_news());
        //exit();

        $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) {
        //echo '<pre>';
        //var_dump($this->news_model->get_news($slug));
        //exit();
        $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');

    }

}

我还是个初学者,所以我的调试解决方案有限。

【问题讨论】:

  • 您使用的是哪个版本的 CI??
  • 我认为你的新闻模型中的 get_news() 方法可能是罪魁祸首。此外,您应该真正将 $slug 变量设置为 null 或其他一些默认值,以防止在没有 slug 的情况下访问页面时出错。公共函数视图($slug = null) { ... }
  • @NiranjanNRaju 我正在通过旧版学习
  • 您的 if( empty 语句引发 404 错误。 $data['news_item'] 未定义
  • @twistedpixel 我会试一试

标签: php codeigniter mamp


【解决方案1】:
$data['news'] = $this->news_model->get_news($slug);

应该是

$data['news_item'] = $this->news_model->get_news($slug);

根据您的其余代码。

【讨论】:

  • 这发生在我们所有人身上:)
  • 我可以查看文章,但是当页面加载时我得到NULL
  • 什么意思?如果您可以查看该文章,您如何获得 NULL? NULL 表示“完全为空;未设置”。
  • 我仍然得到 404,所以我假设它来自那个?
  • 我也转向 laravel,因为文档和教程是最新的,而不是 codeigniter
【解决方案2】:

不要使用 $slug,教程 $slug 设置为 true 当您从 set_news 插入数据时,但我不明白为什么属性类型 varchar slug 可以保存布尔类型。这里是设置变量 $slug 和所有属性到 db 的代码。这里是controller news的代码

public function set_news()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text')
        );

        return $this->db->insert('news', $data);
    }

将方法视图的代码编辑为

public function view($slug = NULL)
    {
            $data['news'] = $this->news_model->get_news();
            //echo print_r($data['news_item']['0']['title'], true);

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

            $data['title'] = $data['news']['0']['title'];

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

并编辑views/news/view.php到

<?php foreach ($news as $news_item): ?>
    <h3><?php echo $news_item['title']; ?></h3>
    <div class="main">
            <?php echo $news_item['text']; ?>
    </div>
    <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p><?php endforeach; ?>

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多