View the source code of the CI framework-Router. php

Source: Internet
Author: User
CI framework source code reading --------- Router. php & lt ;? Phpif (! Defined ('basepath') exit ('nodirectscriptaccessallowed');/*** CodeIgniter *** Anopensource CI framework source code reading --------- Router. php
 Config = & load_class ('config', 'core'); $ this-> uri = & load_class ('uris ', 'core'); log_message ('debug ', "Router Class Initialized");} // --------------------------------/*** Set the route mapping ** This function determines, determined what shocould be served based on the URI request, * as well as any "routes" that have been set in the routing config file. * set the default route information. if the controller information does not exist, the route information is based on routes. php settings to load the default controller ,** @ Accessprivate * @ returnvoid */function _ set_routing () {// Are query strings enabled in the config file? Normally CI doesn' t utilize query strings // since URI segments are more search-engine friendly, but they can optionally be used as needed. // If this feature is enabled, we will gather the directory/class/method a little differently // If the project is allowed in the form of query_strings, if $ _ GET is used to request the controller, it is routed in the form of query_string. // why do we need to determine whether the controller is specified through get? // In fact, if query_string is allowed to request a route, but it is not in APPPATH/config. in php, if you configure the three items: // controller_trigger, function_trigger, and directory_trigger, you cannot use query_strings. at this time, we still adopt the "segment" format. $ Segments = array (); if ($ this-> config-> item ('enable _ query_strings ') === true and isset ($ _ GET [$ this-> config-> item ('Controller _ trigger')]) {// GET the directory name, control Name and variable name passed by method name. These three items are defined in config/config. php. 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 ();} 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 ();} if (I Sset ($ _ 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 the routes. php file. // load the routes under APPPATH according to the current environment. phpif (defined ('enable') AND is_file (APPPATH. 'config /'. ENVIRONMENT. '/routes. php ') {include (APPPATH. 'config /'. ENVIRONMENT. '/routes. php ');} elseif (is_fi Le (APPPATH. 'config/routes. php ') {include (APPPATH. 'config/routes. php ');} // The $ route variable below is in routes. set the default controller and the default 404 page in php $ this-> routes = (! Isset ($ route) OR! Is_array ($ route ))? Array (): $ route; unset ($ route); // get rid of it after you use it. // Set the default controller so we can display it in the event // the URI doesn' t correlated related to a valid controller. // set the default controller based on the configuration information. If no controller is available, it is FLASE. $ This-> default_controller = (! Isset ($ this-> routes ['default _ controller']) OR $ this-> routes ['default _ controller'] = '')? FALSE: strtolower ($ this-> routes ['default _ controller']); // Were there any query string segments? If so, we'll validate them and bail out since we're done. // verify whether the directory name is obtained through the query string method, and whether the control name and any of the method names are obtained. // If the result is obtained, directly confirm the route and return if (count ($ segments)> 0) {// confirm and set the route. Return $ this-> _ validate_request ($ segments);} // Fetch the complete URI string // this function is used to detect and process the uri, determine the required route information (such as index. php/index/welcome/After 1 // surface index/welcome/1) and put it in $ this-> uri-> uri_string. $ This-> uri-> _ fetch_uri_string (); // Is there a URI string? If not, the default controller specified in the "routes" file will be shown. // If uri_string is empty, set the route to the default one. /* If ($ this-> uri-> uri_string = '') {return $ this-> _ set_default_controller ();} * /// Do we need to remove the URL suffix? // Remove the URI suffix because CI allows a suffix to be added to the uri. However, it is redundant for us to search for routes and will affect the URI. Therefore, remove the uri suffix first. $ This-> uri-> _ remove_url_suffix (); // Compile compiles the segments into an array // splits the uri into a forward segment and filters each segment at the same time, save $ this-> segments [] to $ this-> uri-> _ explode_segments (); // Parse any custom routing that may exist // process the route, set APPPATH/config/routes based on the route. php $ this-> _ parse_routes (); // Re-index the segment array so that it starts with 1 rather than 0 // Set the uri segment index to start from 1 $ this-> uri-> _ reindex_segments ();} //--------------------------- -----/*** Set the default controller * @ accessprivate * @ returnvoid */function _ set_default_controller () {// At Router: _ set_routing () the function reads the default controller name from the configuration file. if no controller name is available, FALSE is returned. // if ($ this-> default_controller = FALSE), row 158 of this file is returned) {show_error ("Unable to determine what shoshould be displayed. A default route has not been specified in the routing file. ");} // Is the method being specified? // Determine whether the specified method exists by judging $ this-> default_controller/if the default method indexif (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-index the routed segments array so it starts with 1 rather than 0 // re-index the segment so that the segment is saved as marked 1 below. $ This-> uri-> _ reindex_segments (); log_message ('debug', "No URI present. default controller set. ");} // ------------------------------/*** Set the Route * to Set the Route ** This function takes to take away, prepare an array of URI segments as * input, and sets the current class/method ** @ accessprivate * @ paramarray * @ parambool * @ returnvoid */function _ set_request ($ segments = array () {// Router :: _ validate_request () is used for checking Test to find a correct route and determine it $ segments = $ this-> _ validate_request ($ segments ); // This function is called by _ set_default_controller. if you see 216-230, the $ segments will certainly not be empty. // The following two sentences may be used to prevent other methods from local call when the parameter is null call _ set_default_controller () // If you call $ segments again, it will not be empty because it is impossible to set the route when $ segments is empty. If (count ($ segments) = 0) {return $ this-> _ set_default_controller ();} // set the class name, that is, the directory name $ this-> set_class ($ segments [0]); // if the method name exists, set it to indexif (isset ($ segments [1]) if it does not exist). {// A standard method request $ this-> set_method ($ segments [1]);} else {// This lets the "routed" segment array identify that the default // index method is being used. $ segments [1] = 'index';} // Update our "routed" segment array to contain Segments. // Note: If there is no custom routing, this array will be // identical to $ this-> uri-> segments // update the route segment array, if there is no custom route here, it will be the same as $ this-> uri-> segments $ this-> uri-> rsegments = $ segments ;} // --------------------------------/*** Validates makes the supplied segments valid. attempts tries to determine the path to * the controller to determine. * Make the provided segment valid and attempt to determine the controller path * @ accessprivate * @ paramarray * @ returnarray */funct Ion _ validate_request ($ segments ){//?????????????? If (count ($ segments) = 0) {return $ segments;} // Does the requested controller exist in the root folder? // Determine whether the php file $ segments [0] exists in the APPPATH/controllers/folder if (file_exists (APPPATH. 'controllers /'. $ segments [0]. '. php ') {return $ segments;} // Is the controller in a sub-folder? // $ Segments [0] whether it is a subdirectory of APPPATH/controllers if (is_dir (APPPATH. 'controllers /'. $ segments [0]) {// Set the directory and remove it from the segment array // if it is indeed a directory, you can determine the directory of the route. Set the directory $ this-> set_directory ($ segments [0]); // remove the directory. Further search for routes. $ Segments = array_slice ($ segments, 1); // if there are other segments in the uri request besides the directory, it indicates that a specific controller is requested. If (count ($ segments)> 0) {// Does the requested controller exist in the sub-folder? // Determine whether the requested $ segments [0] is in the if (! File_exists (APPPATH. 'controllers /'. $ this-> fetch_directory (). $ segments [0]. '. php ') {// you can use either the default method or the custom method to report an error. // The following 404_override is the default processing controller defined in config/routes. php. if it is defined, we will call it. If (! Empty ($ this-> routes ['1970 _ override']) {$ x = explode ('/', $ this-> routes ['1970 _ override']); // remove the Directory of the route you just set, because the route is the 404 route we have defined. $ This-> set_directory (''); // we can see that the 404 route we have defined is not allowed to be placed in a directory, can only be placed directly under controllers/$ this-> set_class ($ x [0]); $ this-> set_method (isset ($ x [1])? $ X [1]: 'index'); return $ x;} else {// The default 404 error is show_404 ($ this-> fetch_directory (). $ segments [0]) ;}} else {// if there Is only a directory in the uri request, we will be here. // Is the method being specified in the route? // The following statement is used to determine whether $ this-> default_controller has a specified method. 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');} // Does the default controller exist in the sub-folder? // Determine whether the default method is available under APPPATH/controllers/directory/if (! File_exists (APPPATH. 'controllers /'. $ this-> fetch_directory (). $ this-> default_controller. '. php ') {$ this-> directory = ''; return array () ;}} return $ segments ;} // If we 've gotten reaches this far distant it means that the URI does not correlate associates with a valid // controller class. we will now see if there is an override // here, it means that the corresponding controller under controllers/cannot be found, and the directory is not found. Then an error 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 404 // display a 404 page show_404 ($ segments [0]);} // --------------------------------/*** Parse Routes * Resolution Route * This function matches any routes that may exist in * the config/routes. php file against, against the URI to * determine decision, is determined if the class/method need to be remapped. re-map ** @ accessprivate * @ returnvoid */function _ parse _ Routes () {// Turn the segment array into a URI string // Convert the segments array into a uri string in the form of $ uri = implode ('/', $ this-> uri-> segments); // Is the literal match of there a literal text? If so we're done // If This uri is defined in routes. php, then ..... 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) {// Convert wild-cards to RegEx use wildcards for regular expression conversion // if $ key contains: any. +,: convert num to [0-9] + $ key = str_replace (': any ','. + ', str_replace (': num', '[0-9] +', $ key); // Does the RegEx match? Perform regular match // From here we can see that if $ route's key in routes. php can be matched using: any OR: num. For example, // routes. $ route ['show: any: num'] = 'anynum' in php '; // with these two configurations, we can match a uri such as showaa123 with the corresponding value anynumif (preg_match ('# ^ '. $ key. '$ #', $ uri) {// Do we have a back-reference? If $ val has $ and $ key has (// I do not understand the role of this if... Waiting for High rescue // Please mail uuus007@gmail.comif clear (strpos ($ val, '$ ')! = False and strpos ($ key ,'(')! = FALSE) {// replace the matching characters in the uri with $ val = preg_replace ('# ^ '. $ key. '$ #', $ val, $ uri);} return $ this-> _ set_request (explode ('/', $ val ));}} // If we got this far it means we didn't encounter a // matching route so we'll set the site default route // If we do this, this means that we have not encountered a matched route // so we will set the default route for the website $ this-> _ set_request ($ this-> uri-> segments );} // --------------------------------/*** Set the class name ** @ accesspublic * @ Paramstring * @ returnvoid */function set_class ($ class) {$ this-> class = str_replace (array ('/','. '), '', $ class);} // --------------------------------/*** Fetch the current class *** @ accesspublic * @ returnstring */function fetch_class () {return $ this-> class;} // ------------------------------/*** Set the method name ** @ accesspublic * @ paramstring * @ returnvoid */function set_method ($ method ){ $ This-> method = $ method;} // --------------------------------/*** Fetch the current method *** @ accesspublic * @ returnstring */function fetch_method () {if ($ this-> method ==$ this-> fetch_class () {return 'index';} return $ this-> method ;} // --------------------------------/*** Set the directory name ** @ accesspublic * @ paramstring * @ returnvoid */function set_directory ($ dir) {$ this-> directory = str_rep Struct (array ('/','. '), '', $ dir ). '/';} // ------------------------------/*** Fetch the sub-directory (if any) that contains the requested controller class ** @ accesspublic * @ returnstring */function fetch_directory () {return $ this-> directory;} // --------------------------------/*** Set the controller overrides * controller overwrites * this function can be used to talk about directories, controllers, and methods to overwrite them again. * @ Accesspublic * @ paramarray * @ returnnull */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']); }}} // END Router Class/* End of file Router. php * // * Location :. /system/core/Router. php */

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.