0

我有链接,

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

这是来自用户指南 od CodeIgniter 的代码。单击此代码的链接,它说:

未找到请求的 URL

为什么?

形成的链接是http://localhost/codeignitor/news/view/20.

这是我的控制器:-

<?php
    class news extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
                    $this->load->helper("url");
            $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)
        {
            $data['news'] = $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');
        }
    }
4

2 回答 2

0

您只提供控制器和方法路径。您缺少页面的 URL:

<a href="<?php echo site_url('news/view/'.$news_item['slug']);?>"> View article </a>

检查文档

于 2013-10-25T18:14:30.100 回答
0

正确使用基本 URL 功能:

base_url('news/view/'.$news_item['slug']);
于 2013-10-25T18:14:37.473 回答