在 CodeIgniter 中传递参数


Passing parameters in CodeIgniter

我已经对这个问题进行了大约一个小时的打击。 我看了高看低,但没有什么对我有用。 这应该很简单,我相信是这样。

我正在尝试将 CodeIgniter 中的一些参数传递给 URL,但似乎没有任何效果。 这是我的控制器:

class Form_controller extends CI_Controller {
    public function change($key = NULL) {
        if (is_null($key)) {
            redirect("reset");
        } else {
            echo "Hello world!";
        }
    }
}

这是我的路线:

$route['change/(:any)'] = "form_controller/change/$1";

每次我访问/index.php/change/hello我都会得到字符串"Hello world!"但是当我访问/index.php/change时,我得到了一个404没有找到。

我正在尝试做的是将参数传递给我的控制器,以便检查数据库中的特定键,然后对其执行操作。 如果数据库中不存在该密钥,那么我需要将它们重定向到其他地方。

对此有什么想法吗?

没关系,我想通了。 我最终制作了两条不同的路线来处理它们,如下所示:

$route['change'] = "form_controller/change";
$route['change/(:any)'] = "form_controller/change/$1";

控制器中的函数现在如下所示:

public function change($key = NULL) {
    if (is_null($key)) {
        redirect("reset");
    } else if ($this->form_model->checkKey($key)) {
        $this->load->view("templates/gateway_header");
        $this->load->view("forms/change");
        $this->load->view("templates/gateway_footer");
    } else {
         redirect("reset");           
    }
}

如果有人有更好的解决方案,我全听。 不过这对我有用。

这可能会

对你有所帮助

public function change() {
 $key = $this->uri->segment(3);

http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

这允许您使用 CI URL 帮助程序轻松获取细分

index.php/change/(3RD Segment)这部分将进入该变量$key。

这可能不是您要找的,但如果您尝试传递两个或更多变量,它非常有用,因为您可以从 url 中获取段并将其存储到变量中