简体   繁体   English

新闻项目教程上的Codeigniter错误404

[英]Codeigniter error 404 on news item tutorial

I saw plenty of questions and tried all those fixes similar to this issue but could not get this resolved. 我看到了很多问题,并尝试了所有类似于此问题的修复程序,但未能解决。 Please help. 请帮忙。

I am following codeigniter news item tutorial. 我正在关注codeigniter新闻项目教程。 I displayed news from database. 我显示了来自数据库的新闻。 When I click on a link to view particular news it throws 404. 当我单击链接查看特定新闻时,它会抛出404。

Here is the code: 这是代码:

Controller: 控制器:

    <?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');
    }

}

Model: 模型:

<?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();
    }

}

View: 视图:

news.php 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 view.php

<?php

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

routes: 路线:

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

Base url is blank in config. 基本网址在配置中为空白。

CI version is 2.1.4 (latest) CI版本为2.1.4(最新)

This URL shows all the news items correctly - http://localhost/educon/index.php/news/ 此URL正确显示所有新闻项目http://localhost/educon/index.php/news/

This URL throws 404 - http://localhost/educon/index.php/news/view/n1 此URL引发404- http://localhost/educon/index.php/news/view/n1

The problem is in your routes. 问题出在您的路线上。 Your routes file have the line as 您的路线文件的行为

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

But your url is 但是你的网址是

http://localhost/educon/index.php/news/view/n1

This will redirect to 这将重定向到

http://localhost/educon/index.php/news/view/view/n1

In your view($slug) method you have a parameter. 在您的view($ slug)方法中,您有一个参数。 You dump this $slug in first line of view() and check it. 您将此$ slug放在第一行view()中并进行检查。

I had a similar issue. 我有一个类似的问题。 For me, the routes.php file was fine (by "fine" I mean it matched what the tutorial said to do (also version 2.1.4)), but my views/news/index.php file generated the news article link like so: 对我来说,routes.php文件很好(按“好”,我的意思是它与教程所说的相符(也是版本2.1.4)),但是我的views / news / index.php文件生成了新闻文章链接,例如所以:

<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>

After noting the issue with the generated path, I dropped news from the path and this worked: 注意到生成路径的问题后,我从路径中删除了新闻,此方法有效:

<p><a href="<?php echo $news_item['slug'] ?>">View article</a></p>

So links went from: /news/news/news-item-2 to /news/news-item-2 因此链接从:/ news / news / news-item-2到/ news / news-item-2

And given the tutorial's routing: 并给出了教程的路由:

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

That then made sense. 那才有意义。

I encountered this just now, and I also just copied everything in the tutorial, the thing is, it is in the arrangement of the routing, at first, I just added the new codes in routes.php like this.... 我刚刚遇到了这个问题,我也只是在教程中复制了所有内容,事实是,这是在路由的安排中,起初,我只是像这样在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';

then change it to 然后将其更改为

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

And now it works like a charm... 现在它就像一种魅力...

Using slug without spaces worked for me. 使用没有空格的子弹对我有用。

The 'News Section' part of the tutorial tells you to create some seed records. 本教程的“新闻”部分告诉您创建一些种子记录。 In my case, my slug contained spaces and I got the 404 error. 在我的情况下,子弹包含空格,但出现404错误。 Removing the spaces fixed this. 删除空间固定此。 (CodeIgniter v.3.1.4) (CodeIgniter v.3.1.4)

try to check your link that goes to news listing it should be something like this 尝试检查您指向新闻列表的链接,应该是这样的

<li><a href="<?php echo base_url(); ?>news/index">News</a></li>

you need to change it to read as follows, because i tried it out and its the fix for the problem 您需要将其更改为以下内容,因为我尝试了它,并解决了该问题

<li><a href="<?php echo base_url(); ?>news/index/index">News</a></li>

It will be great if someone with more experience can guide on how to correct this in the route.php 如果有更多经验的人可以在route.php中指导如何更正此问题,那就太好了

below is my route.php 以下是我的route.php

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

note it will work even if you make the second line as: 请注意,即使您将第二行设置为:

$route['news'] = 'news';

and .htaccess is as: 和.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]

best 最好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM