CI框架源碼解讀之URI.php中_fetch_uri_string()函數(shù)用法分析

字號(hào):


    本文實(shí)例講述了CI框架URI.php中_fetch_uri_string()函數(shù)用法。分享給大家供大家參考,具體如下:
    APPPATH/config/config.php中對(duì)于url 格式的擬定。
    $config['uri_protocol'] = 'AUTO';
    這個(gè)配置項(xiàng)目定義了你使用哪個(gè)服務(wù)器全局變量來(lái)擬定URL。 
    默認(rèn)的設(shè)置是auto,會(huì)把下列四個(gè)方式輪詢一遍。當(dāng)你的鏈接不能工作的時(shí)候,試著用用auto外的選項(xiàng)。
    'AUTO'            Default - auto detects 
    'PATH_INFO'        Uses the PATH_INFO 
    'QUERY_STRING'            Uses the QUERY_STRING 
    'REQUEST_URI'        Uses the REQUEST_URI 
    'ORIG_PATH_INFO'    Uses the ORIG_PATH_INFO 
    CI_URI中的幾個(gè)成員變量
    $keyval = array(); //List  of cached uri segments
    $uri_string; //Current  uri string
    $segments //List  of uri segments
    $rsegments = array() //Re-indexed  list of uri segments
    獲取到的current uri string 賦值到 $uri_string ,通過(guò)function _set_uri_string($str)。
    獲取到$str有幾個(gè)選項(xiàng),也就是_fetch_uri_string()的業(yè)務(wù)流程部分了
    一、默認(rèn)
    $config['uri_protocol'] = 'AUTO'
    時(shí),程序會(huì)一次輪詢下列方式來(lái)獲取URI
    (1)當(dāng)程序在CLI下運(yùn)行時(shí),也就是在命令行下php文件時(shí)候。ci會(huì)這么獲取URI
    private function _parse_cli_args()
    {
      $args = array_slice($_SERVER['argv'], 1);
      return $args ? '/' .implode('/',$args) : '';
    }
    $_SERVER['argv'] 包含了傳遞給腳本的參數(shù) 當(dāng)腳本運(yùn)行在CLI時(shí)候,會(huì)給出c格式的命令行參數(shù)
    截取到$_SERVER['argv']中除了第一個(gè)之外的所有參數(shù) 
    如果你在命令行中這么操作
    php d:\wamp\www\CodeIgniter\index.php\start\index
    _parse_cli_args() 返回一個(gè) /index.php/start/index的字符串
    (2)默認(rèn)使用REQUEST_URI來(lái)探測(cè)url時(shí)候會(huì)調(diào)用 私有函數(shù)  _detect_uri()
    (3)如果上面的兩種方式都不能獲取到uri那么會(huì)采用$_SERVER['PATH_INFO']來(lái)獲取
    $path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO']  : @getenv('PATH_INFO');
    if (trim($path, '/')  != '' && $path != "/".SELF)
    {
      $this->_set_uri_string($path);
      return;
    }
    (4)如果上面三種方式都不能獲取到,那么就使用
    $_SERVER['QUERY_STRING']或者getenv['QUERY_STRING']
    $path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
    if (trim($path, '/') != '')
    {
      $this->_set_uri_string($path);
      return;
    }
    (5)上面四種方法都不能獲取到URI,那么就要使用$_GET數(shù)組了,沒(méi)招了
    if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '')
    {
      $this->_set_uri_string(key($_GET));
      return;
    }
    二、在config.php中設(shè)定了:
    $config['uri_protocol']
    那么 程序會(huì)自動(dòng)執(zhí)行相應(yīng)的操作來(lái)獲取uri
    希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。