2015-05-13 40 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