Codeigniter路由导致函数被多次调用

And*_*rej 5 php codeigniter codeigniter-routing

我的类别控制器中有一个名为“插入”的功能。当我通过像这样的 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]  
Run Code Online (Sandbox Code Playgroud)

编辑:

编辑功能的代码:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

Daw*_*son 1

** 编辑 ** http://your.dot.com/insert调用公共函数 insert($arg),但不带 $arg 数据。 http://your.dot.com/insert/使用“index.php”作为 $arg 调用插入。

路线.php

$route['edit/(:any)'] = 'edit/$1'
Run Code Online (Sandbox Code Playgroud)

接受来自查询字符串的任何参数: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/参数

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]
Run Code Online (Sandbox Code Playgroud)