What is the routing mechanism for 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.
If the content of a page is rendered, it needs to be rendered based on the parameters passed on the URL. Many times it may be written like this: xxx.com/xx?c=x&m=x&t=, and the URLs we see are often like this (take the Sina micro-game coffee lover as an example) Game.weibo.com/ilovecoffee .... This URL design looks a little better than the previous one:
If we visit a nonexistent game application, such as game.weibo.com/ilovecoffee222, the following error message will be printed:
Game.weibo.com after the match to the entry, point to a PHP page, and then based on the parameters to access the game application identification, after the database or cache to query the application identity, if it does not exist, output error prompts, if the application exists to load the game Application link address.
Now write a php example, assuming my IP is 192.168.0.33, I added a path named router, followed by "/Module name/method name/Parameter 1 key/parameter 1 value/..."
Address similar to 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 in a folder directory, assuming that all modules now use separate files in the class directory, which is the same as the router peer, as shown in the following illustration:
The second step is the implementation of the routing dispatcher (index.php)
Get the URI of the request, then get the name of the module to load, the name of the method, and make a simple judgment on the URI parameter.
The third step, the preparation of the module
According to the URI above, we are calling the router method under the Hello module, so you can define a file named Hello.class.php in the class directory (note that Linux is case-sensitive)
<?php
class Hello {
private $_name;
Private $_varvalue;
function __construct () {
}
function Router () {
$this->_name = func_get_arg (0);
$this->_varvalue = Func_get_arg (1);
}
function Printresult () {
echo $this->_name;
echo "<p>";
Echo var_dump ($this->_varvalue);
echo "</p>";
}
? >
Similarly, we can write the HA module.
This is the implementation of a very simple URL route distribution function ...
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.