Core file URI. php for source code full analysis of the CI framework

Source: Internet
Author: User
URI provides a lot of methods to help you segment uri_string. you can easily get a certain segment of uri. For example: $ this-& amp; gt; uri-& amp; gt; segment (n) $ this-& amp; gt; uri-& amp; gt; rs

URI provides a lot of methods to help you segment uri_string. you can easily get a certain segment of uri. For example:

$ This-> uri-> segment (N)

$ This-> uri-> rsegment (N)

$ This-> uri-> slash_segment (N)

$ This-> uri-> slash_rsegment (N)

$ This-> uri-> uri_to_assoc (N)


$ This-> uri-> ruri_to_assoc (N)


And so on.
 Config = & load_class ('config', 'core'); log_message ('debug', "URI Class Initialized");} // Obtain uri_stringfunction _ fetch_uri_string () {// $ this-> config-> item ('uri _ Protocol') is used to get config. in php, the configuration of uri_protocol, AUTO is the default if (strtoupper ($ this-> config-> item ('uri _ Protocol') = 'auto ') {// execute if (php_sapi_name () = 'cli 'or defined ('stdin') in command line mode ')) {$ this-> _ set_uri_string ($ this-> _ parse_cli_args (); return ;}// REQUEST_URI method, In most cases, you can obtain if ($ uri = $ this-> _ detect_uri () {$ this-> _ set_uri_string ($ uri); return ;} // PATH_INFO method $ path = (isset ($ _ SERVER ['path _ info'])? $ _ SERVER ['path _ info']: @ getenv ('path _ info'); if (trim ($ PATH ,'/')! = ''& $ Path! = "/". SELF) {$ this-> _ set_uri_string ($ path); return;} // The QUERY_STRING method $ path = (isset ($ _ SERVER ['query _ string'])? $ _ SERVER ['query _ string']: @ getenv ('query _ string'); if (trim ($ path ,'/')! = '') {$ This-> _ set_uri_string ($ path); return;} // try the last method, GET the key name if (is_array ($ _ GET) & count ($ _ GET) = 1 & trim (key ($ _ GET ), '/')! = '') {$ This-> _ set_uri_string (key ($ _ GET); return;} // If uri_string has not been obtained, only $ this-> uri_string = ''; return;} can be returned. // In fact, many of the following operations overwrite the preceding operations. $ uri = strtoupper ($ this-> config-> item ('uri _ Protocol') configured in php ')); // if REQUEST_URI is used, if ($ uri = 'Request _ URI ') {$ this-> _ set_uri_string ($ this-> _ detect_uri (); return ;} elseif ($ uri = 'cli ') {$ this-> _ set_uri_string ($ this-> _ parse_cli_args (); return;} $ path = (isset ($ _ S ERVER [$ uri])? $ _ SERVER [$ uri]: @ getenv ($ uri); $ this-> _ set_uri_string ($ path );} // delimiter // assign function _ set_uri_string ($ str) {// prevents empty characters from being inserted between ASCII characters, such as Java \ 0script. $ str = remove_invisible_characters ($ str, FALSE ); // If the URI contains only a slash we'll kill it $ this-> uri_string = ($ str = '/')? '': $ Str;} // ------------------------------------------------------------------------ // Check URIprivate function _ detect_uri () {// $ _ SERVER ['request _ URI '] and $ _ SERVER ['script _ name'] both exist to use if (! Isset ($ _ SERVER ['request _ URI ']) OR! Isset ($ _ SERVER ['script _ name']) {return '';} $ uri = $ _ SERVER ['request _ URI ']; // remove the same part, and the remaining useful uri part if (strpos ($ uri, $ _ SERVER ['script _ name']) = 0) {$ uri = substr ($ uri, strlen ($ _ SERVER ['script _ name']);} elseif (strpos ($ uri, dirname ($ _ SERVER ['script _ name']) === 0) {$ uri = substr ($ uri, strlen (dirname ($ _ SERVER ['script _ name']);} // allow? /Welcome/index. if (strncmp ($ uri ,'? /', 2) === 0) {$ uri = substr ($ uri, 2) ;}$ parts = preg_split ('#\? # I ', $ uri, 2); $ uri = $ parts [0]; if (isset ($ parts [1]) {$ _ SERVER ['query _ string'] = $ parts [1]; parse_str ($ _ SERVER ['query _ string'], $ _ GET );} else {$ _ SERVER ['query _ string'] = ''; $ _ GET = array ();} if ($ uri = '/' | empty ($ uri) {return '/';} $ uri = parse_url ($ uri, PHP_URL_PATH ); // returns urireturn str_replace (array ('//','.. /'),'/', trim ($ uri ,'/'));}//-------------------------------------------------------- ------------ // Parse the command line parameter private function _ parse_cli_args () {$ args = array_slice ($ _ SERVER ['argv'], 1); return $ args? '/'. Implode ('/', $ args): '';} // ---------------------------------------------------------------- // filter invalid url characters, the allowed uri is your configuration $ config ['permitted _ uri_chars'] = 'A-z 0-9 ~ %.: _ \-'; Function _ filter_uri ($ str) {if ($ str! = ''& $ This-> config-> item ('permitted _ uri_chars ')! = ''& $ This-> config-> item ('enable _ query_strings ') = FALSE) {if (! Preg_match ("| ^ [". str_replace (array ('\-', '\-'), '-', preg_quote ($ this-> config-> item ('permitted _ uri_chars '), '-')). "] + $ | I", $ str) {show_error ('The URI you submitted has disallowed characters. ', 400) ;}// Convert programatic characters to entities $ bad = array (' $ ',' (',') ',' % 28 ', '% 29'); $ good = array (' $ ',' (',') ',' (','); return str_replace ($ bad, $ good, $ str );}//----------------------------------------- ------------------------- // If url_suffix is configured, remove function _ remove_url_suffix () {if ($ this-> config-> item ('URL _ suffix ')! = "") {$ This-> uri_string = preg_replace ("| ". preg_quote ($ this-> config-> item ('URL _ suffix ')). "$ |", "", $ this-> uri_string) ;}/// separator // use/to separate uri_string and save the result to the segments function _ explode_segments () {foreach (explode ("/", preg_replace ("| /*(. + ?) /* $ | "," \ 1 ", $ this-> uri_string) as $ val) {// Filter segments for security $ val = trim ($ this-> _ filter_uri ($ val); if ($ val! = '') {$ This-> segments [] = $ val ;}}// this is to re-index the array and start with subscript 1. this is for your usage habits, for example, $ this-> uri-> segment (1) is used to obtain the first function _ reindex_segments () {array_unshift ($ this-> segments, NULL ); array_unshift ($ this-> rsegments, NULL); unset ($ this-> segments [0]); unset ($ this-> rsegments [0]);} // ---------------------------------------------------------------------- // Obtain a function segment ($ n, $ no_result = FALSE) {return (! Isset ($ this-> segments [$ n])? $ No_result: $ this-> segments [$ n];} // returns a uri fragment function rsegment ($ n, $ no_result = FALSE) after the route is determined) {return (! Isset ($ this-> rsegments [$ n])? $ No_result: $ this-> rsegments [$ n];} function uri_to_assoc ($ n = 3, $ default = array ()) {return $ this-> _ uri_to_assoc ($ n, $ default, 'segment');} function ruri_to_assoc ($ n = 3, $ default = array ()) {return $ this-> _ uri_to_assoc ($ n, $ default, 'rsegment');} function _ uri_to_assoc ($ n = 3, $ default = array (), $ which = 'segment') {if ($ which = 'segment') {$ total_segments = 'total _ segments '; $ segment_array = 'segmen T_array ';} else {$ total_segments = 'total _ rsegments'; $ segment_array = 'rsegment _ array';} if (! Is_numeric ($ n) {return $ default;} if (isset ($ this-> keyval [$ n]) {return $ this-> keyval [$ n];} if ($ this-> $ total_segments () <$ n) {if (count ($ default) = 0) {return array () ;}$ retval = array (); foreach ($ default as $ val) {$ retval [$ val] = FALSE;} return $ retval;} $ segments = array_slice ($ this-> $ segment_array (), ($ n-1); $ I = 0; $ lastval = ''; $ retval = array (); foreach ($ segments as $ seg) {if ($ I % 2) {$ retval [$ las Tval] = $ seg;} else {$ retval [$ seg] = FALSE; $ lastval = $ seg;} $ I ++;} if (count ($ default)> 0) {foreach ($ default as $ val) {if (! Array_key_exists ($ val, $ retval) {$ retval [$ val] = FALSE ;}}} // Cache the array for reuse $ this-> keyval [$ n] = $ retval; return $ retval;} // Obviously, it is used to flip the information in the array into uri_stringfunction assoc_to_uri ($ array) {$ temp = array (); foreach (array) $ array as $ key => $ val) {$ temp [] = $ key; $ temp [] = $ val;} return implode ('/', $ temp );} // use the second parameter to check whether the "/" line function slash_segment ($ n, $ where = 'trailing') is added before and after the uri ') {return $ this-> _ slash_segment ($ n, $ where, 'segment');} function slash_rsegment ($ n, $ where = 'trailing ') {return $ this-> _ slash_segment ($ n, $ where, 'rsegment');} function _ slash_segment ($ n, $ where = 'trailing ', $ which = 'segment') {$ leading = '/'; $ trailing = '/'; if ($ where = 'trailing') {$ leading = '';} elseif ($ where = 'leading') {$ trailing = '';} return $ leading. $ this-> $ which ($ n ). $ trailing;} // returns the uri fragment array function segment_array () {return $ this-> segments;} function rsegment_array () {return $ this-> rsegments ;} // calculate the number of uri fragments. function total_segments () {return count ($ this-> segments);} function total_rsegments () {return count ($ this-> rsegments );} // returns the uri_stringfunction uri_string () {return $ this-> uri_string;} function ruri_string () {return '/'. implode ('/', $ this-> rsegment_array ());}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.