CI框架url模式及得到參數

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

 

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

$this->input->post()
$this->input->get()html

-----------------------------------------------------------------------------------------------------------------------框架

本文主要介紹在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');
 
 
能夠使用相似以下代碼獲取CI的PATH_INFO模式中的參數
public function search(){
    $func_args = func_get_args();
    $func_args_key_value = array();
    if(!empty($func_args)){
        foreach ($func_args as $key=>$val){
            if($key%2==0){
                $func_args_key_value[$val] = isset($func_args[$key+1])?$func_args[$key+1]:'0';
            }
        }
    }

    $is_static = isset($func_args_key_value['is_static'])?$func_args_key_value['is_static']:'0';
    $product_name = isset($func_args_key_value['product_name'])?$func_args_key_value['product_name']:'0';
    $class_id = isset($func_args_key_value['class_id'])?intval($func_args_key_value['class_id']):0;
    $brand_id = isset($func_args_key_value['brand_id'])?intval($func_args_key_value['brand_id']):0;
    $date_count = isset($func_args_key_value['date_count'])?intval($func_args_key_value['date_count']):0;
    $page = isset($func_args_key_value['page'])?intval($func_args_key_value['page']):1;
    $order = isset($func_args_key_value['order'])?intval($func_args_key_value['order']):1;
    $is_promotion_activity = isset($func_args_key_value['is_promotion_activity'])?intval($func_args_key_value['is_promotion_activity']):0;
相關文章
相關標籤/搜索