我看到了很多问题并尝试了所有类似于此问题的修复程序,但无法解决此问题。请帮忙。
我正在关注codeigniter新闻项目教程。我在数据库中显示新闻。当我点击链接查看特定新闻时,它会抛出404。
以下是代码:
控制器:
<?php
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) {
$data['news_item'] = $this->news_model->get_news($slug);
// var_dump($data);exit;
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
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();
}
}
查看:
news.php
<?php
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) {
$data['news_item'] = $this->news_model->get_news($slug);
// var_dump($data);exit;
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');
}
}
view.php
<?php
echo '<h2>' . $news_item['title'] . '</h2>';
echo $news_item['text'];
路线:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
配置中的基本网址为空。
CI版本是2.1.4(最新)
此网址正确显示所有新闻项目 - http://localhost/educon/index.php/news/
此网址引发404 - http://localhost/educon/index.php/news/view/n1
答案 0 :(得分:1)
问题出在你的路线上。您的路线文件的行为
$route['news/(:any)'] = 'news/view/$1';
但你的网址是
http://localhost/educon/index.php/news/view/n1
这将重定向到
http://localhost/educon/index.php/news/view/view/n1
在您的视图($ slug)方法中,您有一个参数。你在第一行视图()中转储这个$ slug并检查它。
答案 1 :(得分:1)
我有类似的问题。对我来说,routes.php文件很好(“很好”,我的意思是它与教程所说的相符(也是2.1.4版)),但是我的views / news / index.php文件生成了新闻文章链接之类的这样:
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
在注意到生成路径的问题之后,我从路径中删除了新闻,这很有效:
<p><a href="<?php echo $news_item['slug'] ?>">View article</a></p>
所以链接从:/ news / news / news-item-2转到/ news / news-item-2
根据教程的路由:
$ route ['news /(:any)'] ='news / view / $ 1';
然后才有意义。
答案 2 :(得分:0)
我刚才遇到过这个问题,我也只是复制了教程中的所有内容,事实上,它是在路由的排列中,首先,我刚才在routes.php
中添加了新的代码。 ...
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['news'] = 'news';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
然后将其更改为
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
现在它就像一个魅力......
答案 3 :(得分:0)
使用没有空格的slu for对我有用。
本教程的“新闻部分”部分告诉您创建一些种子记录。在我的情况下,我的slug包含空格,我得到404错误。删除固定的空格。 (CodeIgniter v.3.1.4)
答案 4 :(得分:0)
尝试检查您的新闻列表链接应该是这样的
<li><a href="<?php echo base_url(); ?>news/index">News</a></li>
您需要将其更改为如下所示,因为我尝试了它并解决了问题
<li><a href="<?php echo base_url(); ?>news/index/index">News</a></li>
如果有经验丰富的人可以在route.php
中指导如何解决这个问题,那将会很棒下面是我的route.php
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news/index';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
请注意,即使您将第二行设为:
,它也能正常工作$route['news'] = 'news';
和.htaccess是:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
最好的