此CodeIgniter URL无效

时间:2013-10-25 18:06:20

标签: php codeigniter url

我有一个链接,

<?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 ?>

这是CodeIgniter用户指南中的代码。点击此代码的链接,它说:

  

找不到请求的网址

为什么?

形成的链接是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');
        }
    }

2 个答案:

答案 0 :(得分:0)

您只提供控制器和方法路径。您错过了网页的网址:

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

检查documentation

答案 1 :(得分:0)

正确使用基本网址功能:

base_url('news/view/'.$news_item['slug']);