【问题标题】:CodeIgniter Tutorial Not WorkingCodeIgniter 教程不工作
【发布时间】:2015-03-21 21:12:38
【问题描述】:

我正在编写 CodeIgniter 初学者教程的后半部分,该教程可在 http://www.codeigniter.com/userguide3/tutorial/news_section.html 找到,与许多其他人一样,我无法使其正常工作。

帖子索引工作正常,但每次我点击“查看文章”时都会收到 404 消息。

我查看了许多关于此的帖子并尝试了许多提供的解决方案,但到目前为止没有任何效果,考虑到我复制并粘贴了教程中的代码以确保我没有这样做,这让我有点发疯不要误抄错了。

我的代码如下:

控制器:

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

    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'] = $this->news_model->get_news($slug);
    if (empty($data['news']))
    {
            show_404();
    }
    $data['title'] = $data['news']['title'];
    $this->load->view('templates/header', $data);
    $this->load->view('news/view', $data);
    $this->load->view('templates/footer');
    }
    }

型号:

class News_model extends CI_Model {

    public function __construct()
    {
            $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();}}

查看:

view.php

echo '<h2>'.$news['title'].'</h2>';
echo $news['text'];

index.php

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

routes.php

$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

提前致谢!

【问题讨论】:

  • 你好,泰莎。对于CodeIgniter forum,这将是一个很好的问题。
  • view() 控制器方法的开始处(即在public function 行之后),输入echo 1; exit() 以查看它是否达到那么远。
  • (顺便说一下,如果您改进缩进,您会发现这更容易使用。有些行的末尾有大括号,而这些应该真的在自己的行上,每个大括号部分是不同级别的嵌套缩进)。
  • 谢谢,我试试看。
  • 哦,我的代码看起来并不像那样,但由于某种原因,当我将它发布到论坛时,它并没有被识别为一段代码,所以我不得不压缩其中的一些.

标签: php codeigniter codeigniter-2


【解决方案1】:

你成功了吗?

主播中的href为你提供了什么?我相信你应该使用site_url()的方法来修复它,所以锚点应该是:

<p><a href="<?= site_url(); ?>news/<?= $news['slug'] ?>">View article</a></p>

我换了

【讨论】:

  • 感谢您的发帖...我决定不再偷懒并阅读手册 ;-)
  • 最后总是这样。 :)
猜你喜欢
  • 2022-08-16
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多