编码器路由导致函数被多次调用


Codeigniter routing causes function to be called multiple times

在我的类别控制器中有一个名为"insert"的函数。当我像这样通过url调用函数:/categories/insert它工作正常,但如果我像这样调用函数:/categories/insert/(斜杠在末尾)函数被调用三次。

即使像这样调用我的编辑函数:/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
    }
}

编辑**http://your.dot.com/insert调用公共函数insert($arg),没有$arg的数据。

http://your.dot.com/insert/使用'index.php'作为$arg调用insert。

routes.php

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

接受来自查询字符串的任何参数:yoursite.com/edit/paramyoursite.com/edit/2
它需要一个名为edit的方法。

如果您使用$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]