If the content of a page is rendered, it needs to be rendered according to the parameters passed on the URL. Many times it is possible to write:xxx.com/xx?c=x&m=x& t=, and the URLs we see are often like this (take the example of a coffee lover in Sina micro-game) Game.weibo.com/ilovecoffee .... This URL design looks a little better than the previous one:)
If we visit a nonexistent game app, such as game.weibo.com/ilovecoffee222, the following error message will be printed:
Game.weibo.com the item that matches, points to a PHP page, then gets the game app ID to access based on the parameter, queries the app ID in the database or cache, and if it doesn't exist, prints out an error message and loads the game app link address if the app exists.
Now write a php example, assuming that my IP is 192.168.0.33, I added a layer named router path, followed by the "/Module name/method name/Parameter 1 of the key/parameter 1 value/...."
An address like this:
192.168.0.33/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http: ............
That is to call the router method in the HA module, and pass in the parameter/a/b/c/d/index after the URL ....
The first step is to intercept the/router/path on the server configuration.
Call the index.php page under a folder directory, assuming that all modules are now stored in a class directory with a separate file, as shown in the router category:
The second step, the implementation of the routing dispatcher (index.php)
1: <! Doctype html>
2:
3:
4: <title> Routing Test ~~</title>
5: <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 "/>
6:
7: <body>
9: <?php
11:date_default_timezone_set ("Asia/shanghai");
13:define (".. /class/");
: $_documentpath = $_server[' document_root '];
: $_filepath = __file__;
: $_requesturi = $_server[' Request_uri '];
", $_filepath); //==>\router\index.php
£ $_urlpath = $_requesturi; //==>/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
At: $_apppatharr = explode (Directory_separator, $_apppath);
/**
* Http://192.168.0.33/router/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
*/hello/router/a/b/c/d/abc/index.html?id=3&url=http:
*/
for ($i = 0; $i < count ($_apppatharr); $i + +) {
: $p = $_apppatharr[$i];
: if ($p) {
: $_urlpath = preg_replace ('/^\/'. $p.'/', $_urlpath, 1);
: }
36:}
A: $_urlpath = preg_replace (", $_urlpath, 1);
Max: $_apppatharr = explode ("/", $_urlpath);
In: $_apppatharr_count = Count ($_apppatharr);
Array
: ' index ',
: ' index ',
: Array ()
47:);
: $arr _url[' controller '] = $_apppatharr[0];
: $arr _url[' method '] = $_apppatharr[1];
and $_apppatharr_count% 2! = 0) {
: Die (' parameter error ');
else {
: For ($i = 2; $i < $_apppatharr_count; $i + = 2) {
: Array (strtolower ($_apppatharr[$i]) =>$_apppatharr[$i + 1]);
: $arr _url[' parms ') = Array_merge ($arr _url[' parms '), $arr _temp_hash);
: }
59:}
: $module _name = $arr _url[' controller ');
A: $module _file = module_dir. $module _name. '. class.php ';
: $method _name = $arr _url[' method '];
if (file_exists ($module _file)) {
: include $module _file;
67:
: new $module _name ();
69:
: if (!method_exists ($obj _module, $method _name)) {
: Die ("The method to invoke does not exist");
MORE: else {
Is_callable: if (array ($obj _module, $method _name )) {
_module: $method _name ($module _name, $arr _url[' parms ') , $obj
75:
: $obj _module-Printresult ();
: }
: }
79:
else {
Bayi: Die ("The defined module does not exist");
82:}
?>:
: </body>
:
Get the URI of the request, then get the module name to load, call the method name, and make a simple judgment on the URI parameter.
The third step, the module writing
According to the above URI, we want to invoke the router method under the Hello module, then we can define a file named Hello.class.php under the class directory (note that Linux is case-sensitive)
1: <?php
Class Hello {
4:
5: private $_name;
6: private $_varvalue;
7:
8: function __construct () {
9:
Ten: }
11:
: Function Router () {
: $this->_name = func_get_arg (0);
: $this->_varvalue = func_get_arg (1);
15:
16:
: function Printresult () {
: echo $this->_name;
: "<p>";
: Echo var_dump ($this->_varvalue);
: "</p>";
: }
23:}
:?>
Similarly, we can write an ha module.
This is the implementation of a very simple URL routing distribution function ...
This article refers to:
Using native PHP to write a routing function like CodeIgniter