Ultimate Explanation of thinkphp routing rules (with pseudo-static)-go to Cainiao-PHP source code

Source: Internet
Author: User
Ultimate Explanation of thinkphp routing rules (with pseudo-static)-required by Cainiao
http://my.oschina.net/u/1178986/blog/201206// Thinkphp route definition rules $ route = array ('news/: action ^ delete | update | insert/: year \ d/: month /: day '=> array ('news/read? Extra =: 2 & status = 1', 'Year =: 2 & month =: 3 & day =: 4'), 'News/: action/: year \ d /: month/: day '=> 'news/read? Year =: 2 & month =: 3 & day =: 4'); $ url =' http://www.test.com/index.php/news/read/2012/2/21/extraparam/test.html '; // Extension name $ extension = 'HTML'; // You can view the details based on the pathinfo definition:http://my.oschina.net/u/1178986/blog/201206// We can see that $ _ SERVER ['path _ info'] = 'news/read/2012/2/21/extraparam/test.html '; $ regx = 'news/read/2012/2/21/extraparam/test.html '; // Loop matching routing rule foreach ($ route as $ key => $ value) {// if the match is successful, if (parseUrlRule ($ key, $ value, $ regx, $ extension) break is not matched ;} /*** @ $ rule string routing rule * @ $ route string rule ing new address * @ $ regx string address bar pathinfo string * @ $ extension stirng pseudo static expansion name * return bool */function parseUrlRule ($ rule, $ route, $ regx, $ e Xtension = null) {// remove the suffix! Is_null ($ extension) & $ regx = str_replace ('. '. $ extension, '', $ regx); // splits routing rules and addresses into arrays, and matches $ ruleArr = explode ('/', $ rule) one by one ); $ regxArr = explode ('/', $ regx); // $ route is transmitted as an array, then the first $ url = is_array ($ route )? $ Route [0]: $ route; $ match = true; // match detection foreach ($ ruleArr as $ key = >$ value) {if (strpos ($ value ,': ') = 0) {if (substr ($ value,-2) =' \ d '&&! Is_numeric ($ regxArr [$ key]) {$ match = false; break;} elseif (strpos ($ value, ^ _ ^ ')) {$ stripArr = explode ('|', trim (strstr ($ value, ^ _ ^ '), ^ _ ^ ')); if (in_array ($ regxArr [$ key], $ stripArr) {$ match = false; break ;}// static items are case insensitive} elseif (strcasecmp ($ value, $ regxArr [$ key])! = 0) {$ match = false; break;} // if the match is successful ($ match) {// write the dynamic variable to the array $ matches, also remove static match items foreach ($ ruleArr as $ key => $ value) {if (strpos ($ value, ':') = 0) {// Obtain the dynamic variable as the array subscript if (substr ($ value,-) = '\') $ matchKey = substr ($ value, 1, -2); elseif ($ pos = strpos ($ value, ^ _ ^ ') $ matchKey = substr ($ value, 1, $ pos-1 ); else $ matchKey = substr ($ value, 1); $ matches [$ matchKey] = array_shift ($ regxArr);} else array_shift ($ reg XArr); // remove static match items} // obtain the value in the array to replace $ values = array_values ($ matches) with the child mode. // replace the regular expression, regular expressions must use 'e' as the modifier $ url = preg_replace ('/:( \ d +)/E',' $ values [\ 1-1] ', $ url); // resolution url format: Group/module/operation? Key1 = value1 & key2 = value2 if (strpos ($ url ,'? ')! = False) {// group/module/operation? Key1 = value1 & key2 = value2 $ arr = parse_url ($ url); $ paths = explode ('/', $ arr ['path']); parse_str ($ arr ['query'], $ queryArr);} elseif (strpos ($ url ,'/')! = False) // group/module/operation) $ paths = explode ('/', $ url); else // key1 = value1 & key2 = value2 parse_str ($ url, $ queryArr); // Obtain the group module operation if (! Empty ($ paths) {$ var ['actionname'] = array_pop ($ paths); $ var ['modulename'] = array_pop ($ paths); if (! Empty ($ paths) {$ groupList = 'Home, admin'; $ temp = array_pop ($ paths); if (in_array ($ temp, explode (',', $ groupList) $ var ['groupname'] = $ temp;} // merged to the GET array for global calling $ _ GET = array_merge ($ _ GET, $ var); // merge the parameter if (isset ($ queryArr) $ _ GET = array_merge ($ _ GET, $ queryArr ); // match the remaining parameter preg_replace ('/(\ w +) \/([^, \/] +)/e' in the url ', '$ tempArr [\' \ 1 \ '] = \' \ 2 \ '', implode ('/', $ regxArr); if (! Empty ($ tempArr) $ _ GET = array_merge ($ _ GET, $ tempArr); // if route is an array (is_array ($ route )) {$ route [1] = preg_replace ('/:( \ d +)/E',' $ values [\ 1-1] ', $ route [1]); parse_str ($ route [1], $ var); $ _ GET = array_merge ($ _ GET, $ var); strpos ($ url ,'? ')! = False? $ Der = '&': $ der = '? '; // The final parameter written to $ _ GET, which consists of three parts. // 1. remaining parameters in the address bar // 2. parameters in the route address // 3. $ route is the second parameter if (! Empty ($ tempArr) $ var = array_merge ($ tempArr, $ var); $ url. = $ der. http_build_query ($ var);} $ _ GET ['finalurl'] = $ url; // ensure that $ _ REQUEST can also be accessed $ _ REQUEST = array_merge ($ _ REQUEST, $ _ GET); // result print_r ($ _ GET); return true;} return $ match;} // index will be set during deployment. php hide, enable the rewrite module of apache // rewrite rule: RewriteRule ^ (. +) $/index. php/$1 // detailed configuration: http://lamp.linux.gov.cn/Apache/ApacheMenu/rewrite/// Apache automatically converts http:/www.test.com/news/read/2012/2/21/extraparam/test.htmlto http:/logs

2. [Image]Sogou February 21 1621_2.png

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.