1. What is the routing mechanism of PHP
1. The routing mechanism is to extract the corresponding parameters of the system from a specific form of URL structure. For example, such as: http://main.test.com/article/1 Where: /article/1 -> ?_m=article&id=1.
2. Then convert the URL with corresponding parameters into a specific form of URL structure, which is the reverse process of the above process.
2. PHP URL routing method
In general: get path information->process path information
URL routing method:
The first method is mapping through url parameters, usually two parameters, representing the controller class and method respectively. For example, index.php?c=index&m=index maps to the index method of the index controller.
The second method is through url-rewrite. The advantage of this is that it can map other suffixes that do not end in php. Of course, the first method can also be achieved through rewrite, but it is also more common to use rewrite purely. , generally you need to configure the
rewrite rules of apache or nginx
Copy code The code is as follows:
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQU EST_FILENAME} !-d
RewriteRule . /index.php [L]
The third way is through pathinfo. The so-called pathinfo is shaped like this url. xxx.com/index.php/c/index/aa/cc, when apache processes this URL, it will input the part after index.php into the environment variable $_SERVER['PATH_INFO'], which is equal to /c/index/ aa/cc. Then our router can analyze it by parsing this string. Where the following parts are put into the parameters will vary according to each framework.
3. A simple PHP routing implementation
3.1 Modify the htaccess file
Write the rewrite file that comes with the server apache or IIS, Import the URL structure into the specified file such as: index.php.
Enable rewrite: The htaccess file is a configuration file in the Apache server. It is responsible for web page configuration in the relevant directory. To enable .htaccess, you need to modify apache/conf/httpd.conf, enable AllowOverride, and use AllowOverride to restrict the use of specific commands.
Copy code The code is as follows:
Options FollowSymLinks
AllowOverride None
;Directory />
Options FollowSymLinks
AllowOverride All
🎜>
Copy code The code is as follows:
RewriteEngine on #rewriteengine is the rewrite engine switch on to open off to close
#RewriteCond $1 !^(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, $1 represents the value matched by the first bracket, and $2 represents the second one.
The above code is to import the URL structure into sharexie/test.php. Just save these as .htaccess files and place them in the root directory of your website.
php
echo 'Your Action is:' . $_GET['action'];
echo '
';
echo 'Your ID is:' . $_GET[' id'];
?>
Okay, let’s type in the browser now:
127.0.0.1/view-12.html
The output is:
Your Action is: view
Your ID is: 12
1. Explain RewriteRule:
RewriteRule is a rewriting rule that supports regular expressions. The above ([0-9]{1,}) refers to being composed of numbers. $ is the end mark, indicating that it ends with a number!
2. RewriteRule configuration parameters
1) R forces external redirection
2) F disables URL and returns 403 HTTP status code.
3) G forces the URL to be GONE and returns 410 HTTP status code.
4) P forces the use of proxy forwarding.
5) L indicates that the current rule is the last rule and stops analyzing the rewriting of future rules.
6) N Run the rewrite process starting from the first rule again.
7) C is associated with the next rule 8) T=MIME-type(force MIME type) Force MIME type
9) NS Only used for non-internal subrequests
10) NC Case-insensitive
11) QSA append request string
12) NE does not output escaped special characters %3d$1 is equivalent to =$1
Example:
1. Set xianglc to index.php?c=myuser&m=itime&domain=xianglc
Copy the code The code is 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 The code is 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, used to parse rules, match and convert URLs.
First transfer all links to index.php, distribute routes in index.php, and assign them to functions in the corresponding class files according to classes and methods. Use $_SERVER['REQUEST_URI'] to take out the part after www.xx.com/ in the URL, and divide it into class, method and parameter key=>value according to relevant rules. Finally, include the class file and execute the functions in it. The example is as follows:
Copy code The code is as follows:
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); //==>routerindex.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('参数错误');
} 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("要调用的方法不存在");
} else {
if (is_callable(array($obj_module, $method_name))) {
$obj_module -> $method_name($module_name, $arr_url['parms']);
$obj_module -> printResult();
}
}
} else {
die("定义的模块不存在");
}
?>
http://www.bkjia.com/PHPjc/759966.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/759966.htmlTechArticle一、什么是php的路由机制 1、路由机制就是把某一个特定形式的URL结构中提炼出来系统对应的参数。举个例子,如:http://main.test.com/article/1...