index.php / news 404未找到

时间:2015-10-29 06:44:21

标签: php codeigniter

我是CodeIgniter的初学者,我正在关注官方网站上的教程。我完成了静态页面。当尝试新闻示例并打开链接index.php/news时,它显示未找到消息404。

应用/控制器/ News.php

<?php

class News extends CI_Controller {

    public function __construct()
        {
                parent::__construct();
                $this->load->model('news_model');
                $this->load->helper('url_helper');
        }

        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_item'] = $this->news_model->get_news($slug);
                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');
        }
}

应用/视图/消息/ index.php的

<h2><?php echo $title; ?></h2>

<?php foreach ($news as $news_item): ?>

        <h3><?php echo $news_item['title']; ?></h3>
        <div class="main">
                <?php echo $news_item['text']; ?>
        </div>
        <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>

<?php endforeach; ?>

应用/视图/消息/ view.php

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

应用/配置/ routes.php文件

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

2 个答案:

答案 0 :(得分:0)

更改此行

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

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

答案 1 :(得分:0)

错误

  

这应该是

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

并将其放在.htaccess(外部应用程序文件夹)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>