亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

實現(xiàn)URL到類方法的映射

original 2019-06-06 10:00:27 421
abstrait:<?php /**  * 實現(xiàn)URL到類方法的映射  */ namespace pig; class Route {     //配置路由信息     protected $route = [];   &nbs
<?php
/**
 * 實現(xiàn)URL到類方法的映射
 */

namespace pig;

class Route
{
    //配置路由信息
    protected $route = [];
    //PATHINFO
    protected  $pathInfo = [];
    //URL參數(shù)
    protected $params = [];
    //構(gòu)造方法
    public function __construct($route)
    {
        //路由初始配置
        $this->route = $route;
    }
    //路由解析
    public function parse($queryStr='')
    {
        //第一步:將字符床前后/去掉,再按分隔符/拆分到數(shù)組中去
        $queryStr = strtolower(trim($queryStr,'/'));
        $queryArr = explode('/',$queryStr);

        //第二步:解析出$queryArr數(shù)組中的內(nèi)容(模塊,控制器,操作,參數(shù))
        switch (count($queryArr))
        {
            //沒有參數(shù),則使用默認的模塊/控制器/操作
            case 0:
                $this->pathInfo =  $this->route;
                break;

            //只有一個參數(shù):用戶只提供了模塊,控制器和操作使用默認值
            case 1:
                $this->pathInfo['module'] = $queryArr[0];
                break;
            //兩個參數(shù):模塊和控制器自定義,操作是默認值
            case 2:
                $this->pathInfo['module'] = $queryArr[0];
                $this->pathInfo['controller'] = $queryArr[1];
                break;
            //三個參數(shù):模塊/控制器/操作全部定義:全部來自用戶的實際請求
            case 3:
                $this->pathInfo['module'] = $queryArr[0];
                $this->pathInfo['controller'] = $queryArr[1];
                $this->pathInfo['action'] = $queryArr[2];
                break;
            //對參數(shù)進行處理
            default:
                $this->pathInfo['module'] = $queryArr[0];
                $this->pathInfo['controller'] = $queryArr[1];
                $this->pathInfo['action'] = $queryArr[2];

                //從pathInfo數(shù)組的索引3開始,將剩余的元素全部作為參數(shù)進行處理
                $arr = array_slice($queryArr,3);
                //鍵值對必須成對出現(xiàn),所以每次遞增為2
                for($i=0;$i<count($arr);$i +=2){
                    //如果沒有第二參數(shù),則放棄
                    if(isset($arr[$i+1])){
                        $this->params[$arr[$i]] = $arr[$i+1];
                    }
                }
                break;
        }
        //返回當(dāng)前路由的實例對象,主要是方便鏈式調(diào)用:$route->parse()->worm()->print()
        return $this;
    }
    //請求分發(fā)
    public function dispatch()
    {
        //生成的帶有命名空間的控制器名稱:app\模塊\controller\控制器
        //類名稱應(yīng)該與類文件所載的絕對路徑一一對應(yīng),這樣才可以實現(xiàn)自動映射,方便自動加載
        //模塊名稱
        $module = $this->pathInfo['module'];

        //控制器名稱
        $controller ='app\\'.$module.'\controller\\' . ucfirst($this->pathInfo['controller']);

        //操作
        $action = $this->pathInfo['action'];
        if(!method_exists($controller,$action)){
          $action = $this->route['action'] ;
          header('Location:/');
        }
        //將用戶的請求分發(fā)到指定的控制器和對應(yīng)的操作方法上
        return call_user_func_array([new $controller,$action],$this->params);
    }

    //獲取pathInfo
    public function getPathInfo()
    {
        return $this->pathInfo;
    }
    //獲取模塊
    public function getModule()
    {
        return $module = $this->pathInfo['module'] ?  : $this->route['module'];
    }
    //獲取控制器
    public function getController()
    {
        return  'app\\' . $this->getModule() . '\controller\\' . ucfirst($this->pathInfo['controller']);
    }

}


Professeur correcteur:查無此人Temps de correction:2019-06-10 09:08:40
Résumé du professeur:完成的不錯。很多框架都是這樣作的,學(xué)習(xí)原理,更好的理解編程。繼續(xù)加油

Notes de version

Entrées populaires