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

路由解析+請求分發(fā)

原創(chuàng) 2019-03-03 17:09:56 568
摘要:/*** 配置文件Config.php*/<?php/** * 配置文件:適用于整個應(yīng)用 * 采用php數(shù)組的方式返回數(shù)據(jù) */return [    //應(yīng)用配置    'app' => [        //調(diào)試開關(guān)        

/**
* 配置文件Config.php
*/

<?php
/**
* 配置文件:適用于整個應(yīng)用
* 采用php數(shù)組的方式返回數(shù)據(jù)
*/
return [
   //應(yīng)用配置
   'app' => [
       //調(diào)試開關(guān)
       'debug' => true,
   ],

   //路由配置
   'route' => [
       //默認(rèn)模塊
       'model' => 'admin',

       //默認(rèn)控制器
       'controller' => 'Index',

       //默認(rèn)操作
       'action' => 'index',
   ],

   //數(shù)據(jù)庫配置
   'db' => [
       //數(shù)據(jù)庫類型
       'database_type' => 'mysql',

       //默認(rèn)的數(shù)據(jù)庫名稱
       'database_name' => 'frame',

       //默認(rèn)主機(jī)名
       'server' => '127.0.0.1',

       //默認(rèn)的用戶名
       'username' => 'root',

       //用戶密碼
       'password' => 'root',

       //默認(rèn)客戶端的字符編碼集
       'charset' => 'utf8',

       //默認(rèn)服務(wù)端口號
       'port' => 3306,
   ],
];

?>


/**
* 路由解析類Route.php
* 1.路由解析
* 2.請求分發(fā)
*/

<?php

namespace config;

class route
{
   //當(dāng)前的路由配置信息
   protected $route = [];

   //PATHINFO
   protected $pathInfo = [];

   //URL中的參數(shù)
   protected $params = [];

   //構(gòu)造方法
   public function __construct($route)
   {
       //用路由配置初始化
       $this->route = $route;
   }

   //解析路由:將url解析到對應(yīng)的模塊,控制器和操作
   public function parse($queryStr='')
   {
       //  /admin/user/add/name/lee/age/30
       //  $this->pathinfo = ['module'=>'admin','controller'=>'user','action'=>'add']
       //  參數(shù)數(shù)組:$this->paras = ['name'=>'lee','age'=>30]

       //第一步:將查詢字符串前后的/去掉,再按分隔符/拆分到數(shù)組中
       $queryStr = trim(strtolower($queryStr),'/');
       $queryArr = explode('/',$queryStr);//explode():把字符串打散為數(shù)組
//        $queryArr = array_filter($queryArr);//array_filter過濾數(shù)組中為空的值

       //第二部:解析出$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ù):模塊/控制器/操作全部來自用戶的實(shí)際請求
           case 3:
               $this->pathInfo['module'] = $queryArr[0];
               $this->pathInfo['controller'] = $queryArr[1];
               $this->pathInfo['action'] = $queryArr[2];
               break;

           //對參數(shù)進(jìn)行處理
           default:
               $this->pathInfo['module'] = $queryArr[0];
               $this->pathInfo['controller'] = $queryArr[1];
               $this->pathInfo['action'] = $queryArr[2];

               //從$pathInfo數(shù)組的第3個索引開始,將剩余的元素全部作為參數(shù)進(jìn)行處理
               $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)前路由器的實(shí)例對象,主要是方便鏈?zhǔn)秸{(diào)用:$route->parse()->worm()
       return $this;

   }

   //請求分發(fā)
   public function disspatch()
   {
       //生成的帶有命名空間的控制器類名稱: app\模塊\controller\控制器類
       //類名稱應(yīng)該與類文件所在的絕對路徑一一對應(yīng),這樣才可以實(shí)現(xiàn)自動映射,方便自動加載
       //模塊名稱
       $module = $this->pathInfo['module'];

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

       //操作名
       $action = $this->pathInfo['action'];
       //method_exists檢查類的方法是否存在
       if(!method_exists($controller,$action)){
           $action = $this->route['action'];
//            header('Location:/');
           return '不存在該操作';
       }

       //將用戶的請求分發(fā)到指定的控制器和對應(yīng)的操作方法上
       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\\'. ucfirst($this->pathInfo['controller']);
   }

   //獲得參數(shù)
   public function getParams()
   {
       return $this->params;
   }
}

?>

批改老師:西門大官人批改時間:2019-03-03 17:31:49
老師總結(jié):路由解析是mvc框架中比較核心的步驟。目的是把用戶的url請求解析映射為類和方法

發(fā)佈手記

熱門詞條