我正在研究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';
提前致谢!
答案 0 :(得分:-1)
你设法解决了吗?
锚中的href
为您提供了什么?我相信您应该使用site_url()
方法来修复它,因此锚点应该是:
<p><a href="<?= site_url(); ?>news/<?= $news['slug'] ?>">View article</a></p>
我交换了