CI框架獲取post和get參數_CodeIgniter心得

請參考:CI文檔的輸入類部分:php

$this->input->post()
$this->input->get()框架

-----------------------------------------------------------------------------------------------------------------------post

本文主要介紹在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');
相關文章
相關標籤/搜索