First, complete the routing class
1 Create a folder for other classes to store the classes we created and then modify the path that called the class
2 when our URL is Www.xxx.com/index/index we want to change his access path to the index controller and the index method, we actually visit the Www.xxx.com/index.php/index/index
3 First we want to hide index.php, get the parameter part of the path to return the corresponding controller and method name
4 we want to create a. htaccess file in the root directory as follows:
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{request_filename}!-f
Rewriterule ^ (. *) $ index.php/$1 [qsa,pt,l]
</IfModule>
5 We can print $_server variables to see if there is the value we want to get, we need is it inside the URL path value, this value area out of the path name is different request_url there are redirect_url variables, When we do not have a parameter variable behind the URL is '/' or no parameter is we give him a judgment if we give him a hint if it's empty, otherwise we define a variable to get the value inside the Request_url and divide the value into our string with the explode () delimiter;
6 Convert the excess part of the URL into get to destroy the used key value above, and then loop for example:
My default controllers and methods are index/index.
The following is the source code
if (Isset ($_server[' Request_uri ')) && $_server[' Request_uri ']!= '/') {
$path = $_server[' Request_uri '];
$patharray = explode ('/', trim ($path, '/'));
if (isset ($patharray [0])) {
$this->controller = $patharray [0];
}
unset ($patharray [0]);
if (Isset ($patharray [1])) {
$this->action = $patharray [1];
Unset ($patharray [1]);
} else {
$this->action = ' index ';
}
$count = count ($patharray) + 2; Definition 2 is because I remove the front index/index and the key starts at 2, so the length is 2.
$i = 2;
while ($i < $count) {
if (Isset ($patharray [$i + 1])) {
$_get[$patharray [$i]] = $patharray [$i + 1];
}
$i = $i + 2;
}
P ($_get);
} else {
$this->controller = ' index ';
$this->action = ' index ';
}