2013-05-10 31 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