abstract:<?php /** * 路由解析類 * 1.路由解析 * 2.請求分發(fā) */ namespace pig; class Route { //路由配置參數(shù) protected $rout
<?php /** * 路由解析類 * 1.路由解析 * 2.請求分發(fā) */ namespace pig; class Route { //路由配置參數(shù) protected $route = []; //PATHINFO解析模塊 protected $pathInfo = []; //為了測試把私有屬性protected修改成public // public $pathInfo = []; //URL參數(shù)數(shù)組 protected $params = []; // public $params = []; //構(gòu)造方法 public function __construct($route) { //用路由配置初始化 $this->route = $route; } //解析路由 public function parse($queryStr='') { // /admin/user/add/name/zhu/age/30 // 解析模塊:$this->pathInfo =['module'=>'admin','controller'=>'user','action'==>'add'] // 參數(shù)數(shù)組:$this->params = ['name'=>'zhu','age'=>30] //第一步:將字符串前后的/去掉,再按分隔符/拆分到數(shù)組中 // http://pig.io/pig/route.php?admin/user/add/name/zhu/age/30 結(jié)果: /admin/user/add/name/zhu/age/30 //1.strtolower($queryStr)轉(zhuǎn)換成小寫,trim去掉前后/ //$queryStr = trim(strtolower($queryStr),'/'); $queryStr = trim(strtolower($queryStr),'/'); //2.用explode('/',$queryStr)切割 $queryArr= explode('/',$queryStr); //3.用array_filter()過濾空值,0會當(dāng)成false //$queryArr = array_filter($queryArr); //第二步:解析出$queryArr數(shù)組中的內(nèi)容(模塊,控制器,操作,參數(shù)) switch(count($queryArr)) { //沒有參數(shù),使用默認(rèn)的模塊/控制器/操作 case 0: $this->pathInfo = $this->route; break; //只有一個參數(shù):只存在模塊,控制器和操作使用默認(rèn)值 case 1: $this->pathInfo['module'] = $queryArr['0']; break; //兩個參數(shù):模塊和控制器自定義,操作是默認(rèn)的 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ù)組的索引開始,將剩余的元素全部作為參數(shù)來處理 $arr = array_slice($queryArr,3); //從第4個元素進行處理,鍵值對必須成對出現(xiàn),每次遞增2,每2個一組進行處理 for($i=0;$i < count($arr);$i += 2){ //如果沒有第二個參數(shù),則放棄 if(isset($arr[$i+1])){ //參數(shù)鍵值對處理 $this->params[$arr[$i]] = $arr[$i+1]; } } break; } //返回當(dāng)前路由的實例對象,主要是方便鏈?zhǔn)秸{(diào)用:$route->parse()->worm() return $this; } //請求分發(fā) public function dispatch() { //生成的帶有命名空間的控制器類名稱:app\模塊\controller\控制器類 //類名稱與類文件所在的絕對路勁一一對應(yīng),實現(xiàn)自動映射,方便加載 //模塊名稱 $module = $this->pathInfo['module']; //控制器名稱 獲取絕對路徑:'app\\' . $module .'\controller\\' . $controller = 'app\\' . $module .'\controller\\' . ucfirst($this->pathInfo['controller']); //die($controller); //操作名 $action = $this->pathInfo['action']; //判斷$controller是否存在$action方法 if(!method_exists($controller,$action)){ $action = $this->route['action']; //不存在跳轉(zhuǎn)到首頁 header('Location:/'); } //存在執(zhí)行將用戶請求分發(fā)到指定的控制器和方法上 return call_user_func_array([new $controller,$action],$this->params); } //獲取pathInfo信息 public function getPathInfo() { return $this->pathInfo; } //獲取模塊 public function getModule() { return $this->pathInfo['module'] ? :$this->route['module']; } //獲取控制器名稱 public function getController() { return 'app\\' . $this->getModule() .'\controller\\'; } } //測試路由 $queryStr = $_SERVER['QUERY_STRING']; echo $queryStr; echo '<hr>'; echo '<pre>'; print_r(explode('/',$queryStr)); echo '<hr>'; $config = require 'config.php'; //實例化Route(),放在變量中 $route = new Route($config['route']); $route->parse($queryStr); //顯示模塊/控制器/方法 print_r($route->pathInfo); //顯示參數(shù) print_r($route->params); //對象的形式調(diào)用 //print_r($route->parse()); //測試請求分發(fā) require __DIR__ .'/../app/admin/controller/Index.php'; echo $route->dispatch();
Correcting teacher:天蓬老師Correction time:2019-04-08 10:12:46
Teacher's summary:你這是把教學(xué)代碼全部復(fù)制上來, 真的懂了嗎