我目前正在学习CodeIgniter的方法,我需要一些关于本教程的视图新闻部分的帮助。它显示了404页面。我的网站是Tristans.tk。点击新闻,然后点击其中一个标题。这是我的代码:
这是我的模特:
class News_model extends CI_Model{
public function __construct(){
$this->load->database();
}
public function get_news($ID=FALSE){
if($ID===FALSE){
$query=$this->db->order_by("post_time", "DESC")->get('news');
return $query->result_array();
}
$query=$this->db->get_where('news',array('ID'=>$ID));
return $query->row_array();
}
}
这是新闻控制员:
class News extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('news_model');
}
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($ID){
$data['news_item'] = $this->news_model->get_news($ID);
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');
}
}
这是观点:
<?php
print_r($news_item);
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['content'];
?>
路线:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
新闻索引:
<?php foreach ($news as $news_item): ?>
<div class='post'>
<h1 class='post_title'><a href="news/view/<?php echo $news_item['ID'] ?>"><?php echo $news_item['title'] ?></a></h1>
<p class='post_time'><?php echo $news_item['post_time'] ?></p>
<p class='post_content'><?php echo $news_item['content'] ?></p>
<span class='comment_link'><a href="news/view/<?php echo $news_item['ID'] ?>">Comment</a></span>
</div>
<?php endforeach ?>
答案 0 :(得分:2)
尝试更改此
$route['news/(:any)'] = 'news/view/$1';
到
$route['news/(:any)'] = 'news/$1';
使用此:$route['news/(:any)'] = 'news/view/$1';
,
如果您访问yourdomain.tk/news/view/1
实际上您访问yourdomain.tk/news/view/view/1
答案 1 :(得分:0)
更改
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['news'] = 'news';
$route['news/create'] = 'news/create';
$route['news/show/(:any)'] = 'news/show/$1';
到
$route['news'] = 'news';
$route['news/create'] = 'news/create';
$route['news/show/(:any)'] = 'news/show/$1';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
祝你好运