Codeigniter - 我不能在网址末尾使用数字

0 php codeigniter

我的主控制器中有以下内容

public function chapter-1 () {
    $data['title'] = "Hello";
    $this->load->view("site_head", $data);
    $this->load->view("site_header");
    $this->load->view("site_content");
    $this->load->view("site_footer");
}
Run Code Online (Sandbox Code Playgroud)

但是当我写"公共功能第一章"时,数字1以橙色亮起,我希望我可以使用第一章到我的网站上获取www.webpage.com/chapter-1.我怎么能完成它?

Mon*_*eus 5

我强烈反对你目前的计划.

以下是一个更易维护和干燥的解决方案:

class Chapter extends CI_Controller
{
    public function view($chapter_num = 1) {

        // Create a model to help you pull data from the database
        $this->load->model('chapter_helper');

        $data = $this->chapter_helper->get_chapter($chapter_num)

        $this->load->view("site_head", $data);
        $this->load->view("site_header");
        $this->load->view("site_content");
        $this->load->view("site_footer");
    }
}
Run Code Online (Sandbox Code Playgroud)

所以现在你可以像这样访问它:

www.example.com/chapter/view/1

www.example.com/chapter/view/2

www.example.com/chapter/view/3

最重要的是,您不需要在控制器内部创建多个功能.