2013-05-10 73 views
3

我在我的類別控制器中有一個名爲「插入」的函數。當我通過這樣的url調用函數時:/ categories/insert它工作正常,但是如果我像這樣調用函數:/ categories/insert /(最後的斜槓),函數被調用三次。Codeigniter路由導致函數被調用多次

即使打電話給我這樣的編輯功能:/ categories/edit/2 - 編輯功能被調用三次。

在config/routes.php中我只有默認路由。我的.htaccess是這樣的:

RewriteEngine on 
RewriteCond $1 !^(index\.php|images|include|robots\.txt) 
RewriteRule ^(.*)$ /index.php/$1 [L] 

編輯:

的編輯功能的代碼:

public function edit($id = '') 
{ 
    $this->load->helper("form"); 
    $this->load->library("form_validation"); 
    $data["title"] = "Edit category"; 

    $this->form_validation->set_rules('category_name', 'Category name', 'required'); 

    if (!$this->form_validation->run()) 
    { 
     $data['category'] = $this->categories_model->get_categories($id); 
     $this->load->view("templates/admin_header", $data); 
     $this->load->view("categories/edit", $data); 
     $this->load->view("templates/admin_footer", $data); 
    } 
    else 
    { 
     $this->categories_model->update($id); 
     // other logic 
    } 
} 
+0

你怎麼能說其所謂的三次呢,你可以發佈您的代碼? – Sudz 2013-05-10 14:24:46

+0

我知道,因爲我對代碼的第一行設置斷點。我用函數的代碼編輯了我原來的帖子,我認爲它與代碼沒有任何關係,因爲當我調用插入函數而不在網址末尾使用斜線時,它可以正常工作。 – Andrej 2013-05-10 15:00:00

+0

你的路線是什麼樣的? – Dawson 2013-05-10 17:55:08

回答

0

**編輯** http://your.dot.com/insert調用公共功能插件($ ARG)無數據爲$ arg。 http://your.dot.com/insert/調用以'index.php'作爲$ arg插入。

routes.php文件

$route['edit/(:any)'] = 'edit/$1' 

接受來自查詢字符串來任何PARAM:yoursite.com/edit/paramyoursite.com/edit/2
它需要一個名爲方法編輯

如果您正在使用$route=['default_controller'] = 'foo',作爲你的所有方法的容器,路線更改爲$route['edit/(:any)'] = 'foo/edit/$1'或類似:$route['(:any)'] = 'foo/$1/$2'爲你的路由的最後一行(注意:這將工作yoursite.com/insert/ PARAMyoursite.com/edit/param

foo.php 

    public function insert() { ... } 

    public function edit($id=null) { ... } 

    /* End of file foo.php */ 


.htaccess 

    RewriteCond $1 !^(index\.php) 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ /index.php?$1 [L] 
+0

如果我只調用一個視圖,它會加載一次函數,就像應該那樣。但我需要這個模板,這是在官方CI教程中完成的方式。這是我的編輯功能。爲什麼我的插入函數被調用一次,如果我沒有斜線在URL的末尾和三次當我把斜槓?我的插入功能也有三個視圖。 – Andrej 2013-05-10 15:22:13

+0

用斜線表示,它可能在尋找'/ index.php',或者該方法試圖在斜線後期望的任何參數下運行。請參閱編輯。 – Dawson 2013-05-10 16:15:13

+0

我只是通過我的base.php控制器查看我的網站。我使用HMVC模式和模板,所以我不像你一樣加載視圖。 – Dawson 2013-05-10 16:16:57