在CodeIgniter中传递参数

时间:2013-10-26 21:30:15

标签: php 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。

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

对此有何想法?

2 个答案:

答案 0 :(得分:2)

没关系,我明白了。我最终制定了两条不同的路线来处理它们,如下:

$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");           
    }
}

如果有人有更好的解决方案,我会全力以赴。这对我有用。

答案 1 :(得分:0)

这可能会帮助你

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

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

这使您可以使用CI网址助手轻松抓取细分

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

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