Home >Backend Development >PHP Tutorial >CI framework source code interpretation of _fetch_uri_string() function usage analysis in URI.php, ciuristring_PHP tutorial
The example of this article describes the usage of _fetch_uri_string() function in CI framework URI.php. Share it with everyone for your reference, the details are as follows:
The formulation of url format in APPPATH/config/config.php.
$config['uri_protocol'] = 'AUTO';
This configuration item defines which server global variables you use to formulate URLs.
The default setting is auto, which will poll the following four methods. When your link doesn't work, try using an option other than 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
Several member variables in 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
The obtained current uri string is assigned to $uri_string through function _set_uri_string($str).
There are several options to get $str, which is the business process part of _fetch_uri_string()
1. Default
$config['uri_protocol'] = 'AUTO'
, the program will poll the following methods once to obtain the URI
(1) When the program is run under CLI, that is, when the php file is under the command line. ci will get URI like this
private function _parse_cli_args() { $args = array_slice($_SERVER['argv'], 1); return $args ? '/' .implode('/',$args) : ''; }
$_SERVER['argv'] contains the parameters passed to the script. When the script is run on the CLI, the command line parameters in c format will be given
Intercept all parameters in $_SERVER['argv'] except the first one
If you do this from the command line
php d:\wamp\www\CodeIgniter\index.php\start\index
_parse_cli_args() returns a string of /index.php/start/index
(2) By default, when REQUEST_URI is used to detect the url, the private function _detect_uri()
will be called.(3) If neither of the above two methods can obtain the uri, $_SERVER['PATH_INFO'] will be used to obtain it
$path = (isset($_SERVER['PATH_INFO'])) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO'); if (trim($path, '/') != '' && $path != "/".SELF) { $this->_set_uri_string($path); return; }
(4) If none of the above three methods can be obtained, then use
$_SERVER['QUERY_STRING'] or getenv['QUERY_STRING']
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING'); if (trim($path, '/') != '') { $this->_set_uri_string($path); return; }
(5) If the above four methods cannot obtain the URI, then you have to use the $_GET array, there is no other way
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '') { $this->_set_uri_string(key($_GET)); return; }
2. Set in config.php:
$config['uri_protocol']
Then the program will automatically perform the corresponding operations to obtain the uri
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.