This article mainly introduces the thinkphp routing rules using examples of detailed understanding and pseudo static function implementation (Apache rewrite), need friends can refer to the following
Code is as follows: <?php //thinkphp routing definition rule $route = array ( ' news/:action/:yeard/:month/:d ay ' => ' News/read?year=:2&month=:3&day=:4 ', ' news/:action^delete|update|insert/:yeard/:month/:d ay ' =>array ( ' News/read?extra=:2&status=1 ', ' year=:2& Month=:3&day=:4 '), ); $url = ' http://www.test.com/index.php/news/read/2012/2/21/extraparam/test.html '; //suffix name $extension = ' html '; //know: $_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 succeeds, do not continue to match if Parseurlrule ($key, $value, $REGX, $extension)) break; //Run results: Print $_get//array// (// [actionname] => read// [modulen AME] => News/ [extra] => 2012 [status] => 1// [extraparam] => Test// [year] => [month] => 2// [day] =>// [finalurl => NEWS/READ?EXTRA=2012&STATUS=1&EXTRAPARAM=TEST&YEAR=2012&MONTH=2&DAY=21// )// [ Finished in 0.6s] //equivalent to access: http://www.test.com/news/read?extra=2012&status=1&extraparam=test& YEAR=2012&MONTH=2&DAY=21 //In Deployment will hide index.php, open the Apache rewrite module//rewrite rule: Rewriterule ^ (. +) $ / index.php/$1 //Open, Apache will automatically convert http:/www.test.com/news/read/2012/2/21/extraparam/test.html to http:/ www.test.com/index.php/news/read/2012/2/21/extraparam/test.html /** * @ $rule string & nbsp; Routing rules * @ $route string rule mapping new address * @ $regx string Address bar PathInfo string * @ $extension stirng pseudo-static extension * &nbsP;return bool */function Parseurlrule ($rule, $route, $REGX, $extension =null) { //remove suffix name !is_null ($extension) && $regx = Str_replace ('. '). $extension, ", $REGX); //The routing rules and addresses into an array, and then matches the $RULEARR = explode ('/', $rule); $REGXARR = explode ('/', $REGX); //$route in array format, take the first $url = Is_array ($route)? $route [0]: $route; $match =true; //Matching detection foreach ($ruleArr as $key => $value) { if strpos ($value, ': ') ===0) { if (substr ($value, -2) = = ' d ' &&!is_numeric ($REGXARR [$key])] { & nbsp; $match = false; break; }elseif (Strpos ($value, ' ^ ')) { $STRIPARR = explode (' | '), TRIM (Strstr ($value ,'^'),'^')); if (In_array ($REGXARR [$key], $STRIPARR)) { $match = False; break; &NBSP, } //static entries are case-insensitive }elseif (strcasecmp ($value, $REGXARR [$key])!==0 { $match = false; break; } &nb Sp &NBSP} //matching success if ($match) { //writes dynamic variables to array $matches while removing static matches & nbsp foreach ($ruleArr as $key => $value) { if (Strpos ($value, ': ') ===0) { //Get dynamic variable, as array subscript if (substr ($value, -2,1) = = ") $matchKe y = substr ($value, 1,-2); ElseIf ($pos =strpos ($value, ' ^ ')) $matchKey =substr ($value, 1 , $pos-1); else $matchKey = substr ($value, 1); $matches [$matchKey] = Array_shift ($REGXARR); &NBsp }else Array_shift ($REGXARR); //removal of static matches &NBSP; //get the values in the array to replace the with the child mode $value s = array_values ($matches); //regular matching replacement, with ' E ' as modifier $url = preg_replace ('/:(d+)/e ', ' $values [1-1] ', $url); //resolution URL format: grouping/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 ']); &NBSP;PARSE_STR ($arr [' query '], $QUERYARR); }elseif (Strpos ($url, '/')!==false) //Group/module/operation) $paths = explode ('/') , $url); else //key1=value1&key2=value2 &NBSP;PARSE_STR ($ URL, $querYarr); //get Group module operations &NBSP;IF (!empty ($paths)) { $var [' A Ctionname '] = 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; &NBSP}  , //merged into get array to facilitate global invocation of $_get = arr Ay_merge ($_get, $var); //merge parameters if (Isset ($QUERYARR)) $_get = Array_merge ($_get, $QUERYARR); //match the remaining parameters in the URL preg_replace ('/w+)/([^,/]+)/e ', ' $tempArr [' 1 ']= ' 2 '], Implode ('/', $REGXARR)); if (!empty ($TEMPARR)) $_geT = Array_merge ($_get, $TEMPARR); //route is an array of words if (Is_array ($route)) { $rout E[1]=preg_replace ('/:(d+)/e ', ' $values [1-1] ', $route [1]); &NBSP;PARSE_STR ($route [1], $var); $_get = Array_merge ($_get, $var); strpos ($url, '? ')! ==false? $der = ' & ': $der = '? '; //final written to $_get parameters, including three parts &NBSP;//1. Address bar remaining parameters &NBSP;//2. Parameters in routing address &NBSP;//3. $route is the second parameter of the array if (!empty ($TEMPARR)) &N Bsp $var = Array_merge ($TEMPARR, $var); $url. = $der. Http_build_query ($var); &NBSP} $_get[' finalurl ' = $url; //Guarantee $_request can also access $_request = Array_merge ($_request,$_get); //results print_r ($_get); return true;   return $match; //below is the regular routing code: $rule = '/news/read/(d+)/(d+)/(d+)/'; $route = ' News/read?year=:1&month=:2&day=:3 '; $REGX = ' news/read/2012/2/21/extraparam/test.html '; $extension = ' html '; PARSEURLRULEREGX ($rule, $route, $REGX, $extension); /** * @ $rule string Routing rules * @ $route string &NB SP; new address for rule map * @ $regx string Address bar pathinfo string * @ $extension stirng pseudo-static extension name * return bool */function parseurlruleregx ($rule, $route, $REGX, $extension =null) { !is_null ($extension) && $regx = Str_replace ('. ') $extension, ", $REGX); $url = Is_array ($route)? $route [0]: $route; if (Preg_match ($rule, $REGX, $matches)) { $url = preg_replace ('/:(d+)/e ', ' $matches [1] ', $url); }else return false; //resolution URL format: grouping/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 ']); &NBSP;PARSE_STR ($arr [' query '], $QUERYARR); }elseif (Strpos ($url, '/')!==false) //Group/module/operation) $paths = explode ('/', $url); else //key1=value1&key2=value2 &NBSP;PARSE_STR ($url, $ Queryarr); //get Group module operations &NBSP;IF (!empty ($paths)) { $var [' actionname '] = array_p OP ($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;   &NBSP} //merged into get array to facilitate global invocation of $_get = Array_merge ($_get, $var); if (Isset ($QUERYARR)) $_get = Array_merge ($_get, $QUERYARR); //matching remaining parameters $REGX = str_replace ($matches [0], ", $REGX); preg_replace ('/(w+)/([^,/]+)/e ', ' $tempArr [' 1 ']= ' 2 ', $regx); if (!empty ($TEMPARR)) { $_get = Array_merge ($_get, $TEMPARR); Strpos ($url, '? ')! ==false? $der = ' & ': $der = '? '; $url. = $der. Http_build_query ($TEMPARR); &NBSP} if (Is_array ($route)) { $route [1] = preg_replace ('/:(d+)/e ', ' $matches [1] ', $route [1]); &NBSP;PARSE_STR ($route [1], $var); if (!empty ($var)) { !empty ($QUERYARR) && $var =array_merge ($QUERYARR, $ var); $_get= array_merge ($_get, $var); &NBSP;} strpoS ($url, '? ')! ==false? $der = ' & ': $der = '? '; $url. = $der. Http_build_query ($var); &NBSP $_get[' finalurl ' = $url; print_r ($_get); $_request = Array_merge ($_get,$_request); return true; //Run results: //array//(// [ActionName] => read// [ModuleName] => News//& nbsp [year] => (/ [month] => 2// [day] =>// [Extraparam] =& Gt Test// [FinalURL] => news/read?year=2012&month=2&day=21&extraparam=test/////[Finished I N 0.1s]