2015-05-13 45 views
0

末使用数字我有以下我的主控制器笨 - 我不能网址

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加入我的网站。我怎样才能完成它?

+9

功能名称不能包含连字符('-')使用下划线。像这个'chapter_1' –

+0

只需将函数名称更改为chapter1并且它应该可以工作。 – Hackerman

回答

5

我强烈建议不要您目前的计划。

下面是一个更容易维护和DRY解决方案:

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

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

3

我同意MonkeyZeus的解决方案,但我不知道他的代码中的控制器名称在哪里。然而,这里是想法你如何可以在网址中使用破折号。在APPPATH . 'config/routes.php'文件,在其添加的底部:

$route['controller-name/chapter-(:num)'] = 'controller_name/chapter_$1'; 

读入documentation如何不覆盖该文件中的最新行。

+0

嘿,我刚刚阅读您的答案,并且我有一个完整的大脑 - 屁。我已经更新了我的答案,这样才有意义。 – MonkeyZeus

+0

哈。没问题。我很害怕这是与chapter_helper的东西,像什么样的帮手是那样的? :) – Tpojka