Use the routing table to match the restful URL, distribute to different action processing, the most basic implementation, only consider routing distribution function
Code as follows: <?php class Router { //routing table Private $routers = Array ( Array ("name" => "userlist", "pattern" => "Get/user", "actio N "=>" User#get "), Array (" name "=>" userinfo "," pattern "=>" get/user/:s " , "Action" => "User#getbyid"), Array ("name" => "Useradd", "pattern" => " Post/user "," Action "=>" User#add "), Array (" name "=>" Userupdate "," pattern "=>" Update/user "," Action "=>" User#update "), Array (" name "=>" Userdel " , "pattern" => "Delete/user/:id", "Action" => "User#delete") ); //Entry Public Function dispatch () { &NB Sp $url = $_server["Request_uri"]; $method = $_server["Request_method"]; foreach ($this->routers as $router) { &NBS P $pattern = $router ["pattern"]; $pats = Explode ("", $pattern); strcasecmp ($pats [0], $method) = 0) { &NB Sp ///current route matching &NBS P $params = $this->checkurl ($method, Strtolower ($url), Strtolower ($pats [1])); if ($params!= null) { & nbsp Array_shift ($params); $action = $router ["Action"]; /Find the first matching route to execute, then return to return $this->invoke ($action, $params); & nbsp } echo "404 Error"; //error 404 } PR Ivate function Invoke ($action, $params) { $acts = explode ("#", $action); $className = $acts [0]. " Action "; $methodName = $acts [1]; $actionDir = dirname (__file__). Directory_separator. " Action "; //Loading action file &NBsp $classFile = $actionDir. Directory_separator $className. ". PHP "; if (! file_exists ($classFile)) { &NBSP ; //404 error echo "404 error, no action found"; return; } else { require $class File "; //Using reflection execution method &NBSP ; $RC = new Reflectionclass ($className); if (! $RC->hasmethod ($methodName)) { &NBSP ; //404 error &NBS P echo "404 error, no method found";   return; } else { & nbsp $instance = $RC->newinstance (); $method = $RC->getmethod ($methodName); $method->invokeargs ($instance, $params); } } //Regular match check, and extract parameters Private function Checkurl ($metho D, $str, $pattern) { //echo "Check $str with $pattern <br>"; $ma = array (); $pattern = LTrim (RTrim ($pattern, "/")); $pattern = "/". Str_rePlace ("/", "/", $pattern). " /?$/"; $pattern = Str_replace (": S", "([^/]+)", $pattern); //echo "pattern $pattern <br>"; //$str = "/". $str. " $/"; if (Preg_match ($pattern, $str, $ma) > 0) { return $ma; return null; } ?>