請參考:CI文檔的輸入類部分: $this->input->post() $this->input->get() ----------------------------------------------------------------------------------------------------------------------- 本文主要介紹在CodeIgniter框架中如何獲取get和post參數。 獲取get數據 在PHP主流的框架中,CI中url的pathinfo傳遞參數是一個特殊情況,它沒有使用傳統pathinfo的'c/m/key/value' 這種模式,而是在URI類中封裝了segment這個方法,假設uri為/index.php/welcome/index/phptest/5,在控制器中調用如下
echo $this->uri->segment(3);//輸出phptest echo $this->uri->segment(4);//輸出5 echo $this->uri->segment(1);//welcome
值得注意的是,在控制器中使用$_GET['phptest']是得不到5這個值的。 另外,針對get參數還可以在控制的動作(方法)加參數,例如
class Welcome extends CI_Controller { public function index($id=0, $name=''){ echo $id.$name; } }
上面在index方法里加了兩個參數$id和$name,有默認值表示該參數可選,uri的格式如下
index.php/welcome/index/5/phptest
這里傳入參數的順序不能顛倒。 獲取post數據 在CI控制其中可以直接使用PHP中的$_POST['key']來獲取post數據; 另外CI還封裝了一個Input類,里面提供post方法來獲取post提交過來的數據。
$this->input->post('key');