天天看點

CI架構擷取post和get參數_CodeIgniter心得

請參考: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/phpjyz/5,在控制器中調用如下

echo $this->uri->segment(3);//輸出phpjyz

echo $this->uri->segment(4);//輸出5

echo $this->uri->segment(1);//welcome

值得注意的是,在控制器中使用$_GET['phpjyz']是得不到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/phpjyz

這裡傳入參數的順序不能颠倒。

擷取post資料

在CI控制其中可以直接使用PHP中的$_POST['key']來擷取post資料;

另外CI還封裝了一個Input類,裡面提供post方法來擷取post送出過來的資料。

$this->input->post('key');如何聯系我:【萬裡虎】www.bravetiger.cn

【QQ】3396726884 (咨詢問題100元起,幫助解決問題500元起)

【部落格】http://www.cnblogs.com/kenshinobiy/