CI架構源碼解讀之URI.php中_fetch_uri_string()函數用法分析_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了CI架構URI.php中_fetch_uri_string()函數用法。分享給大家供大家參考,具體如下:

APPPATH/config/config.php中對於url 格式的擬定。

$config['uri_protocol'] = 'AUTO';

這個設定項目定義了你使用哪個伺服器全域變數來擬定URL。
預設的設定是auto,會把下列四個方式輪詢一遍。當你的連結不能工作的時候,試著用用auto外的選項。

'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中的幾個成員變數

$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 ,通過function _set_uri_string($str)。

擷取到$str有幾個選項,也就是_fetch_uri_string()的商務程序部分了

一、預設

$config['uri_protocol'] = 'AUTO'

時,程式會一次輪詢下列方式來擷取URI

(1)當程式在CLI下運行時,也就是在命令列下php檔案時候。ci會這麼擷取URI

private function _parse_cli_args(){  $args = array_slice($_SERVER['argv'], 1);  return $args ? '/' .implode('/',$args) : '';}

$_SERVER['argv'] 包含了傳遞給指令碼的參數 當指令碼運行在CLI時候,會給出c格式的命令列參數

截取到$_SERVER['argv']中除了第一個之外的所有參數 

如果你在命令列中這麼操作

php d:\wamp\www\CodeIgniter\index.php\start\index

_parse_cli_args() 返回一個 /index.php/start/index的字串

(2)預設使用REQUEST_URI來探測url時候會調用 私人函數  _detect_uri()

(3)如果上面的兩種方式都不能擷取到uri那麼會採用$_SERVER['PATH_INFO']來擷取

$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數組了,沒招了

if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != ''){  $this->_set_uri_string(key($_GET));  return;}

二、在config.php中設定了:

$config['uri_protocol']

那麼 程式會自動執行相應的操作來擷取uri

更多關於CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)架構進階教程》、《php優秀開發架構總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork架構入門教程》、《php物件導向程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於CodeIgniter架構的PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.