Core File (route) Router. php

Source: Internet
Author: User
The URI routing mode of CI is clear to everyone. It also supports powerful route redirection:/*** Router ** @ linkhttp: // www.phpddt.com */classCI_Ro

The URI routing mode of CI is clear to everyone. It also supports powerful route redirection:

/*** Router ** @ link http://www.phpddt.com */Class CI_Router {// configuration class var $ config; // route information, routes. var $ routes = array ();/*** List of error routes *** @ var array * @ access public */var $ error_routes = array (); // Current controller name or class name var $ class = ''; // Current controller method var $ method = 'index'; // directory name var $ directory = ''; // Default controller var $ default_controller;/*** Constructor ** Runs the route mapping function. */function _ construct () {$ this-> config = & load_class ('config', 'core '); $ This-> uri = & load_class ('uri', 'core'); log_message ('debug', "Router Class Initialized ");} // ----------------------------------------------------------------------/*** set route ing */function _ set_routing () {$ segments = array (); // if the URL is enable_query_stringsif ($ this-> config-> item ('enable _ query_strings ') === true and isset ($ _ GET [$ this-> config-> item ('Controller _ trigger')]) {// Obtain the directory name if (isset ($ _ GET [$ this-> Config-> item ('directory _ trigger')]) {$ this-> set_directory (trim ($ this-> uri-> _ filter_uri ($ _ GET [$ this-> config-> item ('directory _ trigger')]) )); $ segments [] = $ this-> fetch_directory ();} // Obtain the controller name if (isset ($ _ GET [$ this-> config-> item ('Controller _ trigger')]) {$ this-> set_class (trim ($ this-> uri-> _ filter_uri ($ _ GET [$ this-> config-> item ('Controller _ trigger')]) )); $ segments [] = $ this-> fetch_class ();} // Obtain the method name if (isset ($ _ GET [$ this-> config-> item ('function _ trigger')]) {$ this-> set_method (trim ($ this-> uri-> _ filter_uri ($ _ GET [$ this-> config-> item ('function _ trigger ')]) )); $ segments [] = $ this-> fetch_method () ;}// load routes. php, introduce the route configuration information if (defined ('environment ') AND is_file (APPPATH. 'config /'. ENVIRONMENT. '/routes. php ') {include (APPPATH. 'config /'. ENVIRONMENT. '/routes. php ');} elseif (is_file (APPPATH. 'config/routes. php ') {includ E (APPPATH. 'config/routes. php');} // Obtain route configuration information $ this-> routes = (! Isset ($ route) OR! Is_array ($ route ))? Array (): $ route; unset ($ route); // Get the configuration information to get the default controller $ this-> default_controller = (! Isset ($ this-> routes ['default _ controller']) OR $ this-> routes ['default _ controller'] = '')? FALSE: strtolower ($ this-> routes ['default _ controller']); // if $ segments is obtained, the route is determined directly, after the execution of the function if (count ($ segments)> 0) {// _ validate_request (), return $ this-> _ validate_request ($ segments );} // Obtain and confirm uri_string =. php $ this-> uri-> _ fetch_uri_string (); // if uri_string is empty, only if ($ this-> uri-> uri_string = '') is routed by default '') {return $ this-> _ set_default_controller ();} // CI allows the suffix url_suffix to be added to the uri, but it is useless for routing analysis. remove $ this-> uri-> _ Remove_url_suffix (); // use/to separate uri_string and save the result to segments $ this-> uri-> _ explode_segments (); // Parse the route (check the redirection and set the route) $ this-> _ parse_routes (); // this is to re-index the array from subscript 1, this is for your usage habits. for example, $ this-> uri-> segment (1) is to get the first section $ this-> uri-> _ reindex_segments ();} // Configure/*** set the default route */function _ set_default_controller () {// if no default controller exists, show errorif ($ this-> default_controller = FALSE) {show_error ("Unable to determine what shoshould be displayed. A default route has not been specified in the routing file. ");} // Oh, originally, routes. in php, $ route ['default _ controller'] = "welcome"; you can also set the default method $ route ['default _ controller'] = "welcome/index "; if (strpos ($ this-> default_controller ,'/')! = FALSE) {$ x = explode ('/', $ this-> default_controller); $ this-> set_class ($ x [0]); $ this-> set_method ($ x [1]); $ this-> _ set_request ($ x);} else {$ this-> set_class ($ this-> default_controller ); $ this-> set_method ('index'); $ this-> _ set_request (array ($ this-> default_controller, 'index');} // re-create the segments index, start with $ this-> uri-> _ reindex_segments (); log_message ('debug', "No URI present. default controller set. ");}//------------ ----------------------------------------------------/*** Set routing */function _ set_request ($ segments = array () {// _ validate_request () returns array (controller, method, xxx, xxx ...) $ segments = $ this-> _ validate_request ($ segments); if (count ($ segments) = 0) {// _ set_default_controller () also calls _ set_request () return $ this-> _ set_default_controller () ;}$ this-> set_class ($ segments [0]); if (isset ($ segments [1]) {// A sta Ndard method request $ this-> set_method ($ segments [1]);} else {// If method is not set, then index is the URI in the default method $ segments [1] = 'index';} // URI: $ rsegments is the route after processing and confirmation, segment Information of the actually called Route $ this-> uri-> rsegments = $ segments;} // parts/*** verify the segments array */function _ validate_request ($ segments) {if (count ($ segments) = 0) {return $ segments;} // whether the requested controller exists if (f Ile_exists (APPPATH. 'controllers /'. $ segments [0]. '. php ') {return $ segments;} // if the controller file does not exist, whether it is in the if (is_dir (APPPATH. 'controllers /'. $ segments [0]) {// sets the directory and removes it from the segment array $ this-> set_directory ($ segments [0]); $ segments = array_slice ($ segments, 1); if (count ($ segments)> 0) {// if the current controller file does not exist, show 404if (! File_exists (APPPATH. 'controllers/'. $ this-> fetch_directory (). $ segments [0].'. php') {if (! Empty ($ this-> routes ['1970 _ override']) {$ x = explode ('/', $ this-> routes ['1970 _ override']); $ this-> set_directory (''); $ this-> set_class ($ x [0]); $ this-> set_method (isset ($ x [1])? $ X [1]: 'index'); return $ x;} else {show_404 ($ this-> fetch_directory (). $ segments [0]) ;}} else {// if the $ segments array is empty, use the default route if (strpos ($ this-> default_controller ,'/')! = FALSE) {$ x = explode ('/', $ this-> default_controller); $ this-> set_class ($ x [0]); $ this-> set_method ($ x [1]);} else {$ this-> set_class ($ this-> default_controller ); $ this-> set_method ('index');} // if the default controller file does not exist, an empty array if (! File_exists (APPPATH. 'controllers /'. $ this-> fetch_directory (). $ this-> default_controller. '. php ') {$ this-> directory = ''; return array () ;}// if the controller file is in a subdirectory, return the segments array that does not contain the directory return $ segments;} // This step shows that the controller file does not exist and the 404 page is reported. if (! Empty ($ this-> routes ['1970 _ override']) {$ x = explode ('/', $ this-> routes ['1970 _ override']); $ this-> set_class ($ x [0]); $ this-> set_method (isset ($ x [1])? $ X [1]: 'index'); return $ x;} // Nothing else to do at this point but show a 404show_404 ($ segments [0]);} // ----------------------------------------------------------------------/*** config/routes. php route redirection */function _ parse_routes () {// Convert the uri setment array to uri_string $ uri = implode ('/', $ this-> uri-> segments); // routes. php whether to reset the route if (isset ($ this-> routes [$ uri]) {return $ this-> _ set_request (explode ('/', $ this -> Routes [$ uri]);} // Loop through the route array looking for wild-cardsforeach ($ this-> routes as $ key => $ val) {// CI custom: num and: any wildcard $ key = str_replace (': any ','. + ', str_replace (': num', '[0-9] +', $ key); // Does the RegEx match? If (preg_match ('# ^ '. $ key. '$ #', $ uri) {// regular expressions support reverse applications such as: $ route ['products/([a-z] +)/(\ d +) '] = "$1/id _ $2"; if (strpos ($ val,' $ ')! = False and strpos ($ key ,'(')! = FALSE) {$ val = preg_replace ('# ^ '. $ key. '$ #', $ val, $ uri);} // return $ this-> _ set_request (explode ('/', $ val) ;}}// this step indicates that the redirection route $ this-> _ set_request ($ this-> uri-> segments) is not matched );} function set_class ($ class) {$ this-> class = str_replace (array ('/','. '), '', $ class);} function fetch_class () {return $ this-> class;} function set_method ($ method) {$ this-> method = $ method;} function fetch_method () {if ($ this-> me Thod = $ this-> fetch_class () {return 'index';} return $ this-> method;} function set_directory ($ dir) {$ this-> directory = str_replace (array ('/','. '), '', $ dir ). '/';} function fetch_directory () {return $ this-> directory;} function _ set_overrides ($ routing) {if (! Is_array ($ routing) {return;} if (isset ($ routing ['Directory']) {$ this-> set_directory ($ routing ['Directory']);} if (isset ($ routing ['controller']) AND $ routing ['controller']! = '') {$ This-> set_class ($ routing ['controller']);} if (isset ($ routing ['function']) {$ routing ['function'] = ($ routing ['function'] = '')? 'Index': $ routing ['function']; $ this-> set_method ($ routing ['function']); }}}

 

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.