【问题标题】:This CodeIgniter URL is not working此 CodeIgniter URL 不起作用
【发布时间】:2013-10-25 18:06:19
【问题描述】:

我有一个链接,

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

【问题讨论】:

  • 这是代码,不是链接。向我们展示实时示例或生成的 HTML。
  • codeignitor/news/view/20 是链接,格式是否正确
  • 不工作是什么意思?你的班级/控制器怎么样?你有什么映射吗?发布您要映射到的class/controller
  • 看到我有一个名为“news”的类,它有一个包含一个参数的函数“view”,现在你可以知道,链接是否正确?
  • &lt;a href="news/view/ 更改为 &lt;a href="view/... 我之前在该教程中遇到过这个问题。

标签: php codeigniter url


【解决方案1】:

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

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

检查documentation

【讨论】:

    【解决方案2】:

    正确使用base URL函数:

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

    【讨论】:

    • base_url 不起作用,因为它为您提供了您的站点基本 URL,而没有附加 index_page 或 url_suffix
    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    相关资源
    最近更新 更多