CodeIgniter:我能够列出这些项目但我无法加载一项

时间:2013-05-07 14:04:43

标签: php codeigniter slug codeigniter-routing

应用程序/ htaccess的

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

应用/配置/ 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();
    }
}
?>

应用程序/ controlers / 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

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

我在这里缺少什么?

3 个答案:

答案 0 :(得分:3)

链接应该是:

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

答案 1 :(得分:0)

Controller的索引方法应为:

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>

答案 2 :(得分: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 at:
http://localhost:8080/demo/news/slug-here