我的主控制器中有以下内容
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");
}
但是当我写“公共功能第1章”时,数字1以橙色亮起,我希望我可以使用第1章到我的网站上获取www.webpage.com/chapter-1。我怎么能完成它?
答案 0 :(得分: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");
}
}
所以现在你可以像这样访问它:
www.example.com/chapter/view/1
www.example.com/chapter/view/2
www.example.com/chapter/view/3
最重要的是,您不需要在控制器内部创建多个功能。
答案 1 :(得分:3)
我同意MonkeyZeus'解决方案虽然我不知道他的代码中的控制器名称在哪里。但是,这里有一个想法如何在URL中使用破折号。在APPPATH . 'config/routes.php'
文件中,在其底部添加:
$route['controller-name/chapter-(:num)'] = 'controller_name/chapter_$1';
在documentation中读取如何不覆盖此文件中的最新行。