PHP pseudo-static: php simple pseudo-static URL mechanism implementation

Source: Internet
Author: User

Once upon a time, our company was ready to develop a new set of station systems, decided to KO the previous framework, and to develop a new framework to adapt to the new system features. Leaders do not want to use the outside framework, claiming to develop their own characteristics of the framework (not understand the development of the leadership to kill people). So we put into the new development.
Because our system supports pseudo static, the previous system is the direct use of server Apache or IIS with the rewrite file definition rules, and the framework does not have any routing mechanism, so this framework is ready to use the new policy, PHP implementation of routing mechanism. So I began to explore the way of functional realization.
Before developing, I understand what ' routing mechanism ' is going to do, it mainly does two things.
1. Routing mechanism is to extract a specific form of the URL structure of the system corresponding parameters. For example: http://www.cxybl.com/article/1:/article/1->? _m=article&id=1.
2. Secondly, it is the reverse process of the above process to convert a URL with corresponding parameter to a specific form of URL structure. Because the routing mechanism isolates the transformation relationship between the URL structure and the parameters, the subsequent structure changes will not affect the execution of the following code.
With the above understanding, you can draw a few steps to write a routing mechanism:
1. Write the server Apache or IIS from the rewrite file, the URL structure to import index.php.
2. A Routing rule configuration file.
3. A routing parser for parsing rules, matching and converting URLs.
So, we all realize each of these parts.
1.rewrite file writing, take Apache as an example:
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewriterule ^index\.php$-[l]
Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
Rewriterule (. +) index.php/$1 [l]
</ifmodule>
The code above is to import the URL structure into the index.php, the specific rewrite details will not be detailed.
2. Set up a routing rule configuration file in PHP routes.php, I simply used a hash array to write the rules:
/**
* Routing configuration File Description:
* Routing is configured in an array, a record represents a rule
* Where the data for the array key represents a matching path format: Using a specific string identifier such as: '/{id} '
* string can contain specific variables, all variables are wrapped with braces {}
* Array value is an array of arrays, which is the specific processing of variables in the path in the key
The variable is written in the key of the array, and 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 '), where ' id ' => '/\d+/' is a form of judgment,
* indicates that the ID variable can only be a number, the format can only use regular expressions after the judge, because PHP has no regular classes, so I specify the '/xxx/' and ' #xxx # ' format string is a regular expression
* 2. Default parameters: For example, '/{id} ' => array (' ID ' => '/\d+/', ' _m ' => ' FrontPage ', ' _a ' => ' index '), where ' _m ' => ' FrontPage ' is a default parameter,
* Because the previous path does not have _m and _a information, the default parameters are used as values for _m and _a
*
* So for the rule '/{id} ' => array (' ID ' => '/\d+/', ' _m ' => ' FrontPage ', ' _a ' => ' index '). My incoming/3 system will be converted into index.php?_m=frontpage&_a=index&id=3
*
* Rule matches are matched in the order of the $routes array, and are not matched once the match is made. So some specific matching rules have to be put in front, and the generic is in the back.
* Otherwise, it may cause no specific matching rules to 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 ()
);
3. The most complex and important part of the routing mechanism is the parser.
The parser consists of two classes (the name may not be good).
One is route, as the entire parser external interface, used to parse the rules, matching and transformation of the URL, but it is only a proxy, the actual operation is not directly done by it directly.
One is Routepattern, each Routepattern instance corresponds to one record in the rule array, one route instance contains multiple Routepattern, and all operations in route invoke all internal Routepattern instance operations. and to integrate.
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); This article links http://www.cxybl.com/html/wlbc/Php/20120607/28511.html

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.