【问题标题】:Codeigniter index pageCodeigniter 索引页面
【发布时间】:2012-02-28 21:06:10
【问题描述】:

我想创建一个页面,我可以像这样浏览它:/page/5

我创建了一个page.php 控制器,我可以查看这个页面,这是它的内容:

类页面扩展 CI_Controller {

public function __construct()
{
    parent::__construct();
}

public function index()
{
    $this->load->view('template/header');

    $id = $this->uri->segment(2);
    $this->load->view('template/middle', array('id' => $id));
    $this->load->view('template/footer');
}

}

问题是我无法从 uri 获取页码。

要明确:

这是可以访问的:index.php/page

但在这个网址,我得到的是 404:index.php/page/5

我知道我可以创建另一个控制器函数来查看像 /index.php/page/id/5 这样的页面,但我希望它可以通过索引函数访问。

这可能吗?

【问题讨论】:

    标签: codeigniter uri


    【解决方案1】:

    CodeIgniter 不知道您是在寻找 5() 还是 index(5)

    要解决此问题,请尝试在 config/routes.php 中设置自定义路由:

    $route['page/(:num)'] = 'page/index/$1';
    

    另外,在你的索引函数中:

    public function index($id)
    {
        $this->load->view('template/header');
        $this->load->view('template/middle', array('id' => $id));
        $this->load->view('template/footer');
    }
    

    【讨论】:

    • 很好,谢谢 :-)。使用public function index(*$id*)$id = $this->uri->segment(2);有什么区别?
    • 没问题,你仍然可以使用分段,我只是认为参数更漂亮:P
    • 如果你做public function index($id),你必须传入一个像这个page/index/1这样的id。如果您尝试访问页面/索引,它将无法正常工作。
    • @Catfish 他仍然可以将其设为可选:index($id=false)
    • 我知道,但他问index($id)$this->uri->segment() 有什么区别,所以我只是想澄清一下。
    猜你喜欢
    • 2023-03-11
    • 2012-12-18
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多