0

我的

应用程序/.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

应用程序/config/routes.php

$route['default_controller'] = "news";
$route['404_override'] = '';

应用程序/模型/news_model.php

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

    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)
    {
        echo $slug;
        $data['news_item'] = $this->news_model->get_news($slug);
        var_dump($data);
        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:

<?php foreach ($news as $news_item): ?>
    <?php var_dump($news_item); ?>
    <h2><?php echo "<pre>"; echo $news_item['title'] ?></h2>
    <div id="main">
        <?php echo $news_item['text'] ?>
    </div>
    <p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>

<?php endforeach ?>

和 Applications/views/view.php

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

问题是我可以看到索引(其中列出了我的新闻),但是当我单击 slug 链接时,它会尝试转到:

/news/slug1

它会触发一个未找到的错误..

我在这里想念什么?

4

3 回答 3

3

链接应该是:

 <p><a href="/news/view/<?php echo $news_item['slug'] ?>">View article</a></p>
于 2013-05-07T14:09:49.717 回答
0

控制器的索引方法应该是:

public function index()
{
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';

    $this->load->view('templates/header', $data);
    $this->load->view('view', $data);
    $this->load->view('templates/footer');
}

并且文章链接必须是:

<p><a href="news/index/<?php echo $news_item['slug'] ?>">View article</a></p>
于 2013-05-07T14:15:00.600 回答
0

假设您的 CI 在 /demo 文件夹中:
试试这个 htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/

#Needed for CodeIgniter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|pub|tmp|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

您的操作应该可以通过以下网址访问:
http://localhost:8080/demo/news/view/slug-here
NOT
http://localhost:8080/demo/news/slug-here

于 2013-05-07T14:28:48.320 回答