thinkphp routing rules using the example detailed and pseudo static function implementation (Apache rewrite) _php instance

Source: Internet
Author: User

Copy Code code as follows:

<?php
thinkphp Routing definition Rules
$route = Array (
' news/:action/:year\d/:month/:d ay ' => ' news/read?year=:2&month=:3&day=:4 ',
' news/:action^delete|update|insert/:year\d/: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 ';

Looping matching routing rules
foreach ($route as $key => $value) {
If the match succeeds, the match is not continued
if (Parseurlrule ($key, $value, $REGX, $extension))
Break
}

Run Result: Print $_get
Array
//  (
[ActionName] => Read
[ModuleName] => News
[Extra] => 2012
[Status] => 1
[Extraparam] => test
[Year] => 2012
[Month] => 2
[Day] => 21
[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

The index.php is hidden when deployed, and the Apache rewrite module is turned on
Rewrite rule: rewriterule ^ (. +) $/index.php/$1
When turned on, 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 Routing rules
* @ $route new address for string rule mapping
* @ $regx string Address bar pathinfo strings
* @ $extension stirng pseudo-static extension name
* return bool
*/
function Parseurlrule ($rule, $route, $REGX, $extension =null) {
Remove suffix name
!is_null ($extension) && $regx = Str_replace ('. ') $extension, ", $REGX);

Split the routing rules and addresses into an array and then match them by item
$RULEARR = explode ('/', $rule);
$REGXARR = explode ('/', $REGX);

$route is passed in the form of an array, 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 not case-sensitive
}elseif (strcasecmp ($value, $REGXARR [$key])!==0) {
$match = false;
Break
}
}

  //Match succeeded
   if ($match) {
     //writes dynamic variables to array $matches while removing static matches
     foreach ($ruleArr as $key => $value) {
       if ( Strpos ($value, ': ') ===0) {
        //get 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); To remove a static match
}


Gets the value in the array, in order to replace it with the child mode
$values = Array_values ($matches);
Regular match substitution, with ' e ' as modifier
$url = preg_replace ('/:(\d+)/e ', ' $values [\\1-1] ', $url);

     //parsing url    format:  grouping/module/operation? key1=value1&key2=value2
      if Strpos ($url, '? ')! ==false) {  
      //grouping/module/operation? key1=value1&key2=value2
        $arr = Parse_url ($url);
        $paths = explode ('/', $arr [' path ']);
       parse_str ($arr [' query '], $QUERYARR);
     }elseif (Strpos ($url, '/')!==false)  //grouping/modules/operations)
         $paths = explode ('/', $url);
     else        //key1=value1&key2=value2
       parse_str ($url, $QUERYARR);


Get 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 into a get array for easy global invocation
$_get = Array_merge ($_get, $var);

Merging 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.
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 = '? ';
Parameters that are eventually written to the $_get, including three parts
1. Remaining parameters of Address bar
2. Parameters in the routing address
3. $route is the second parameter of the array
if (!empty ($TEMPARR))
$var = Array_merge ($TEMPARR, $var);
$url. = $der. Http_build_query ($var);
}
$_get[' finalurl ' = $url;
Ensure $_request can also access
$_request = Array_merge ($_request,$_get);
Results
Print_r ($_get);
return true;
}
return $match;
}


The following is the regular route 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 new address for string rule mapping
* @ $regx string Address bar pathinfo strings
* @ $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: grouping/module/operation? key1=value1&key2=value2
if (Strpos ($url, '? ')! ==false) {
Grouping/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);


Get 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 into a get array for easy global invocation
$_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;
}

Run Result:
Array
// (
[ActionName] => Read
[ModuleName] => News
[Year] => 2012
[Month] => 2
[Day] => 21
[Extraparam] => test
[FinalURL] => news/read?year=2012&month=2&day=21&extraparam=test
// )
[Finished in 0.1s]

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.