PHP processing restful requests for routing class sharing

Source: Internet
Author: User

  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";         &nbsp           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;        }    ?>

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.