-
-
- RewriteEngine On
- RewriteRule ^index.php$ - [L]
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteRule (.+) index.php/$1 [L]
Copy code
The above code is to import the URL structure into index.php. The specific rewrite details will not be described in detail.
2. Set up a routing rule configuration file routes.php in PHP. I simply used a hash array to write the rules:
-
- /**
- * Instructions for writing routing configuration files:
- * Routing configuration is in an array array, and one record represents a rule
- * The data of the array key represents the matching path format: use a specific string identifier such as: '/{id}'
- * The string can contain specific variables, all variables are wrapped in braces {}
- * The array value is an array array, which performs specific processing on the variables in the path in the key
- * The variables are written in the key of the array, The specification is written in the value of the array, such as: array('id'=>'/d+/','_m'=>'frontpage','_a'=>'index')
- * The specification is divided into two categories :
- * 1. Format judgment: For example, '/{id}'=> array('id'=>'/d+/','_m'=>'frontpage','_a'=>'index '), for example, 'id'=>'/d+/' is a format judgment,
- * means that the id variable can only be a number, and only regular expressions can be used after the format judgment. Since PHP does not have a regular class, I Specify strings in the format of '/XXX/' and '#XXX#' as regular expressions
- * 2. Default parameters: For example, '/{id}'=> array('id'=>'/d+/' ,'_m'=>'frontpage','_a'=>'index') for example, where '_m'=>'frontpage' is a default parameter,
- * because the previous path does not have _m and _ a information, so the default parameters will be used as the values of _m and _a later
- *
- * So for the rule '/{id}'=> array('id'=>'/d+/','_m' =>'frontpage','_a'=>'index'). When I pass in /3, the system will convert it into index.php?_m=frontpage&_a=index&id=3
- *
- * The rule matching is to match one by one in the order of the $routes array. Once matched, it will not match downwards. Therefore, some specific matching rules should be placed at the front and general ones at the back.
- * Otherwise, specific matching rules may not be executed
- */
- $routes= array(
- '/' => array('_m'=>'wp_frontpage','_a'=>'index'),
- '/{id}'=> array('id'=>'/d+/','_m'=>'wp_frontpage','_a'=>'index'),
- '/{_m }/{_a}/{id}'=> array('id'=>'/d+/'),
- '/{_m}/{_a}'=> array()
- );
Copy code
3. The most complex and important part of the routing mechanism is the parser.
The parser consists of two classes (perhaps poorly named).
One is Route, which is the external interface of the entire parser and is used to parse rules, match and convert URLs. However, it is just a proxy, and the actual operation is not performed directly by it.
One is RoutePattern. Each RoutePattern instance corresponds to a record in the rule array. A Route instance contains multiple RoutePatterns, and all operations in Route will call all internal RoutePattern instance operations and integrate them.
-
- class Route
- {
- private static $instance = null;
- private $routepatterns=array();
-
- private function __construct()
- {
- $routes = array();
- include ROOT."/ routes.php";
- foreach($routes as $key=>$value){
- $this->routepatterns[]=new RoutePattern($key,$value);
- }
-
- if(!isset($ _SERVER['PATH_INFO'])) return false;
- $urlpath= $_SERVER['PATH_INFO'];
- $ismatch=$this->match_url($urlpath);
- $strip_urlpath=str_replace('/','' ,$urlpath);
- if(!$ismatch&&!emptyempty($strip_urlpath)){
- Content::redirect(PAGE_404);
- }
- }
-
- /**
- * Use routing rules to match the corresponding url address. If the match is successful, put the corresponding url parameters into $_GET
- * @param string url address
- * @return bool Whether the match is successful
- */
- public function match_url($urlpath) {
- foreach($this->routepatterns as $router){
- $urlargs=$router->match_url($urlpath);
- if($urlargs!==false){
- $_GET=array_merge($urlargs, $_GET);
- return true;
- }
- }
- return false;
- }
-
- public function rewrite_url($urlparams){
- foreach($this->routepatterns as $router){
- $urlstr=$router-> ;rewrite_url($urlparams);
- if($urlstr!==false){
- return $urlstr;
- }
- }
- $actualparams=array();
- foreach($urlparams as $arg=>$val){
- $actualparams[]=$arg."=".urlencode($val);
- }
- $actualparamstr=implode('&', $actualparams);
- $rewriteurl="/index.php";
- if(! emptyempty($rewriteurl))$rewriteurl.="?{$actualparamstr}";
- return $rewriteurl;
- }
-
- public static function init()
- {
- if (null == self::$instance) {
- self ::$instance = new Route();
- }
- return self::$instance;
- }
- }
-
- class RoutePattern{
- //...
- }
Copy code
About routing The main details of configuration file parsing are all in the RoutePattern class. Regarding the details of rule parsing, URL matching and URL conversion in RoutePattern, the space and energy are limited, so we will not introduce them in detail today. We will analyze them in detail next time.
|