程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php處理restful請求的路由類分享

php處理restful請求的路由類分享

編輯:PHP綜合

復制代碼 代碼如下:
<?php
    class Router {
        // 路由表
        private $routers = array(
            array("name"=>"userlist", "pattern"=>"get /user", "action"=>"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")
        );

        // 入口
        public function dispatch() {
            $url = $_SERVER["REQUEST_URI"];
            $method = $_SERVER["REQUEST_METHOD"];

            foreach ($this->routers as $router) {
                $pattern = $router["pattern"];
                $pats = explode(" ", $pattern);
                if (strcasecmp($pats[0], $method) == 0) {
                    // 是否與當前路由匹配
                    $params = $this->checkUrl($method, strtolower($url), strtolower($pats[1]));
                    if ($params != null) {
                        array_shift($params);
                        $action = $router["action"];
                        // 尋找到第一個匹配的路由即執行,然後返回
                        return $this->invoke($action, $params);
                    }
                }
            }

            echo "404 error";
            // error 404
        }

        private function invoke($action, $params) {
            $acts = explode("#", $action);
            $className = $acts[0]."Action";
            $methodName = $acts[1];
            $actionDir = dirname(__FILE__).DIRECTORY_SEPARATOR."action";

            // 載入action文件
            $classFile = $actionDir.DIRECTORY_SEPARATOR.$className.".php";
            if (! file_exists($classFile)) {
                // 404 error
                echo "404 error, no action found";
                return;
            } else {
                require "$classFile";
                // 使用反射執行方法
                $rc = new ReflectionClass($className);
                if (! $rc->hasMethod($methodName)) {
                    // 404 error
                    echo "404 error, no method found";
                    return;
                } else {
                    $instance = $rc->newInstance();
                    $method = $rc->getMethod($methodName);
                    $method->invokeArgs($instance, $params);
                }
            }
        }

        // 正則匹配檢查,並提取出參數
        private function checkUrl($method, $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;
        }
    }
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved