This article describes how to use thinkphp routing rules and how to implement pseudo-static functions (apache rewrite). For more information, see
The code is as follows:
// Thinkphp route definition rules
$ Route = array (
'News/: action/: year \ d/: month/: day' => 'news/read? Year =: 2 & month =: 3 & day =: 4 ',
'News/: action ^ delete | update | insert/: year \ d/: month/: day' => 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
$ Extension = 'HTML ';
// You can see that $ _ SERVER ['path _ info'] = 'news/read/2012/2/21/extraparam/test.html ';
$ Regx = 'news/read/2012/2/21/extraparam/test.html ';
// Cyclically match routing rules
Foreach ($ route as $ key => $ value ){
// If the match is successful, the match will not continue
If (parseUrlRule ($ key, $ value, $ regx, $ extension ))
Break;
}
// Run the result: print $ _ GET
// Array
//(
// [ActionName] => read
// [ModuleName] => news
// [Extra] = & gt; 2012
// [Status] => 1
// [Extraparam] => test
// [Year] = & gt; 2012
// [Month] => 2
// [Day] => 21
// [FinalUrl] => news/read? Extra = 2012 & status = 1 & extraparam = test & year = 2012 & month = 2 & day = 21
//)
// [Finished in 0.6 s]
// Access equivalent: http://www.test.com/news/read? Extra = 2012 & status = 1 & extraparam = test & year = 2012 & month = 2 & day = 21
// Index. php is hidden during deployment and the apache rewrite module is enabled.
// Rewrite rule: RewriteRule ^ (. +) $/index. php/$1
// After it is enabled, apache automatically converts http:/www.test.com/news/read/2012/2/21/extraparam/test.htmlto http:/response
/**
* @ $ Rule string routing rule
* @ $ Route the new address mapped by the string rule
* @ $ Regx string pathinfo string in the address bar
* @ $ Extension stirng pseudo-static extension name
* Return bool
*/
Function parseUrlRule ($ rule, $ route, $ regx, $ extension = null ){
// Remove the suffix
! Is_null ($ extension) & $ regx = str_replace ('.'. $ extension, '', $ regx );
// Divide the routing rules and addresses into arrays and match them one by one.
$ RuleArr = explode ('/', $ rule );
$ RegxArr = explode ('/', $ regx );
// $ Route is transmitted as an array, and the first route is used.
$ 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;
}
}
// The static items are case insensitive.
} Elseif (strcasecmp ($ value, $ regxArr [$ key])! = 0 ){
$ Match = false;
Break;
}
}
// Match successful
If ($ match ){
// Write dynamic variables to the array $ matches, and remove static matching items
Foreach ($ ruleArr as $ key => $ value ){
If (strpos ($ value, ':') = 0 ){
// Obtain dynamic variables as array subscript
If (substr ($ value,-2, 1) = '\\')
$ 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 ($ regxArr); // remove static match items
}
// Obtain the value in the array to replace it with the sub-mode
$ Values = array_values ($ matches );
// Replace the regular expression with the regular expression. the regular expression must use 'e' as the modifier.
$ Url = preg_replace ('/:( \ d +)/E',' $ values [\ 1-1] ', $ url );
// Parse 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 group module operations
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 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 );
// If route is an array
If (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 in the array.
If (! Empty ($ tempArr ))
$ Var = array_merge ($ tempArr, $ var );
$ Url. = $ der. http_build_query ($ var );
}
$ _ GET ['finalurl'] = $ url;
// Ensure that $ _ REQUEST can be accessed
$ _ REQUEST = array_merge ($ _ REQUEST, $ _ GET );
// Result
Print_r ($ _ GET );
Return true;
}
Return $ match;
}
// The following is the regular routing code:
$ Rule = '/news \/read \/(\ 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 rule
* @ $ Route the new address mapped by the string rule
* @ $ Regx string pathinfo string in the address bar
* @ $ 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;
// Parse 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 group module operations
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 );
If (isset ($ queryArr ))
$ _ GET = array_merge ($ _ GET, $ queryArr );
// Match the 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 );
}
If (is_array ($ route )){
$ Route [1] = preg_replace ('/:( \ d +)/E',' $ matches [\ 1] ', $ route [1]);
Parse_str ($ route [1], $ var );
If (! Empty ($ var )){
! Empty ($ queryArr) & $ var = array_merge ($ queryArr, $ var );
$ _ GET = array_merge ($ _ GET, $ var );
}
Strpos ($ url ,'? ')! = False? $ Der = '&': $ der = '? ';
$ Url. = $ der. http_build_query ($ var );
}
$ _ GET ['finalurl'] = $ url;
Print_r ($ _ GET );
$ _ REQUEST = array_merge ($ _ GET, $ _ REQUEST );
Return true;
}
// Running result:
// Array
//(
// [ActionName] => read
// [ModuleName] => news
// [Year] = & gt; 2012
// [Month] => 2
// [Day] => 21
// [Extraparam] => test
// [FinalUrl] => news/read? Year = 2012 & month = 2 & day = 21 & extraparam = test
//)
// [Finished in 0.1 s]