One, what is the routing mechanism of PHP
1, the routing mechanism is a specific form of the URL structure extracted from the system corresponding parameters. For example: http://main.test.com/article/1:/article/1->? _m=article&id=1.
2, then the corresponding parameters of the URL to convert to a specific form of the URL structure, is the process above the reverse process.
Second, PHP URL routing method
Overall it is: Get path information-> process path information
URL Routing method:
The first is the way to map through the URL parameters, generally two parameters, respectively, representing the Controller class and methods such as Index.php?c=index&m=index map to the index controller index method.
The second, is through the Url-rewrite way, the advantage is that you can achieve a non-PHP end of the other suffixes to map, of course, through the rewrite can also achieve the first way, but the pure use of rewrite is also more common, the general need to configure Apache or Nginx
Rewrite rules
Copy Code code as follows:
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewritebase/
Rewriterule ^index\.php$-[L]
Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
Rewriterule. /index.php [L]
</IfModule>
The third kind, is through the pathinfo way, the so-called pathinfo, is the form like such URL. Xxx.com/index.php/c/index/aa/cc,apache in processing this URL will be the index.php part of the following input to the environment variable $_server[' path_info ', which equals/c/index/aa/ Cc. Our routers then parse the string to analyze it, and the rest of the parts into the parameters of where, based on the different frameworks and different.
三、一个 Simple PHP Routing implementation
3.1 Modifying htaccess files
Write the server Apache or IIS rewrite file, import the URL structure into the specified file such as: index.php.
Opening the Rewrite:htaccess file is a configuration file in the Apache server that is responsible for the page configuration in the related directory. Enable. htaccess, you need to modify apache/conf/httpd.conf, enable allowoverride, and you can use allowoverride to limit the use of specific commands.
Copy Code code as follows:
<directory/>
Options FollowSymLinks
AllowOverride None
</Directory>
To
Copy Code code as follows:
<directory/>
Options FollowSymLinks
AllowOverride All
</Directory>
And then I wrote this rewrite:
Copy Code code as follows:
Rewriteengine on #rewriteengine为重写引擎开关on为开启off为关闭
#RewriteCond $!^ (index.php\.php|images|robots\.txt)
Rewriterule ([A-za-z]{1,})-([0-9]{1,}). html$ sharexie/test.php?action=$1&id=$2
# ([A-za-z]{1,})-([0-9]{1,}). html$ is the rule, sharexie/test.php?action=$1&id=$2 is the format to be replaced, the value that represents the first bracket match, and the second one for $.
The code above is to import the URL structure into the sharexie/test.php. Save these as. htaccess files in the root directory of your Web site.
test.php
Copy Code code as follows:
<?php
Echo ' Your action is: '. $_get[' action '];
Echo ' <br/> ';
Echo ' Your ID is: '. $_get[' id '];
?>
Okay, now we're going to type in the browser:
127.0.0.1/view-12.html
The output is:
Your action is: view
Your ID is: 12
1, explain the Rewriterule:
Rewriterule is an overriding rule that supports regular expressions, and the above ([0-9]{1,}) is a number of digits, and $ is the ending sign, indicating that it ends with a number!
2, rewriterule configuration parameters
1) R Force external redirection
2) F disables the URL and returns a 403HTTP status code.
3 G force URL to gone, return 410HTTP status code.
4) P enforces the use of proxy forwarding.
5) L indicates that the current rule is the last rule, and the rewrite of the rule is stopped after parsing.
6) N rerun the rewrite process starting with the first rule.
7 C is associated with the next rule 8) T=mime-type (Force MIME type) force MIME type
9 NS only for not internal child requests
NC is case insensitive
One) QSA Append request string
NE does not output escape special characters \%3d$1 equivalent to =$1
Example:
1, will XIANGLC will be scheduled to INDEX.PHP?C=MYUSER&M=ITIME&DOMAIN=XIANGLC
Copy Code code as follows:
Rewriterule ^ ([a-za-z0-9]) {6,20}/?$ index.php?c=myuser&m=itime&domain=$0 [L]
2. #RewriteRule ^/index.html$/1.php [L]
Copy Code code as follows:
Rewriterule ^/index-(. *?) -(.*?) -(.*?) -(.*?) -(.*?) -(.*?) -(.*?) -(.*?) -(.*?) $ $9&a=$1&b=$2&c=$3&d=$4&e=$5&f=$6&g=$7&h=$8 [C,NC]
Rewriterule ^ (. *?) -(.*?) -(.*?) -(.*?) -(.*?) -(.*?). HTML (. *?) $/1.php?$7&i=$1&j=$2&k=$3&l=$4&m=$5&n=$6 [QSA,L,NC]
3.2 A routing parser for parsing rules, matching and converting URLs.
First, all the links are transferred to index.php, routed in index.php, and the classes and methods are assigned to functions in the corresponding class file. Use $_server[' Request_uri ' to remove the part of the URL that follows the www.xx.com/, which is divided into class and Mothod and parameter key=>value values according to the relevant rules. Finally, include the class file, and perform the functions therein. Examples are as follows:
Copy Code code as follows:
<?php
error_reporting (0);
Date_default_timezone_set ("Asia/shanghai");
$_documentpath = $_server[' Document_root '];
$_requesturi = $_server[' Request_uri '];
$_urlpath = $_requesturi;
$_filepath = __file__;
$_apppath = Str_replace ($_documentpath, ', $_filepath); ==>\router\index.php
$_apppatharr = Explode (Directory_separator, $_apppath);
for ($i = 0; $i < count ($_apppatharr); $i + +) {
$p = $_apppatharr[$i];
if ($p) {
$_urlpath = preg_replace ('/^\/'. $p. ' \//', '/', $_urlpath, 1);
}
}
$_urlpath = preg_replace ('/^\//', ', ', $_urlpath, 1);
$_apppatharr = Explode ("/", $_urlpath);
$_apppatharr_count = Count ($_apppatharr);
$arr _url = Array (
' Controller ' => ' Sharexie/test ',
' method ' => ' index ',
' Parms ' => Array ()
);
$arr _url[' controller '] = $_apppatharr[0];
$arr _url[' method '] = $_apppatharr[1];
if ($_apppatharr_count > 2 and $_apppatharr_count% 2!= 0) {
Die (' parameter error ');
} else {
for ($i = 2; $i < $_apppatharr_count; $i + + 2) {
$arr _temp_hash = Array (Strtolower ($_apppatharr[$i]) =>$_apppatharr[$i + 1]);
$arr _url[' parms '] = Array_merge ($arr _url[' parms '), $arr _temp_hash);
}
}
$module _name = $arr _url[' controller '];
$module _file = $module _name. Class.php ';
$method _name = $arr _url[' method '];
if (file_exists ($module _file)) {
Include $module _file;
$obj _module = new $module _name ();
if (!method_exists ($obj _module, $method _name)) {
Die ("The method to invoke does not exist");
} else {
if (is_callable array ($obj _module, $method _name)) {
$obj _module-> $method _name ($module _name, $arr _url[' parms '));
$obj _module-> Printresult ();
}
}
} else {
Die ("The defined module does not exist");
}
?>