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

路由解析與分發(fā)基礎(chǔ)理解

原創(chuàng) 2019-03-13 11:04:39 245
摘要:index.php文件中的內(nèi)容 $config=[     'route'=>[         'module'=>'admin',        
index.php文件中的內(nèi)容

$config=[
    'route'=>[
        'module'=>'admin',
        'controller'=>'Index',
        'action'=>'index'
    ]
];

class Route
{
//    默認路由
    private $route=[];
//    默認控制器
    private $pathinfo=[];
//    默認參數(shù)
    private $params=[];
//    實例化時,配置默認路由
    public function __construct($route)
    {
        $this->route=$route;
    }
    public function parse($path='')
    {
//        獲取路由
//        $path=$_SERVER['QUERY_STRING'];
//        1.去掉首位的/
//        2.全部變成小寫
//        3.根據(jù)/切割成數(shù)組
//        4.去掉數(shù)組兩側(cè)的空值
        $path=trim($path,'/');
        $path=strtolower($path);
        $path=explode('/',$path);
//        $path=array_filter($path);
//        echo '<pre>'.print_r($path,true).'</pre>';
        /*
         *       判斷$path的長度
         *          0:沒有參數(shù)部分時進入首頁
         *          1、2、3:分別代表只有模塊、控制器、操作
         *          其他:代表有參數(shù),將前三個刪除,對參數(shù)部分單獨處理
         * */
        switch (count($path)){
            case 0:
                $this->pathinfo=$this->route;
                break;
            case 1:
                $this->pathinfo['module']=$path[0];
                break;
            case 2:
                $this->pathinfo['module']=$path[0];
                $this->pathinfo['controller']=$path[1];
                break;
            case 3:
                $this->pathinfo['module']=$path[0];
                $this->pathinfo['controller']=$path[1];
                $this->pathinfo['action']=$path[2];
                break;
            default:
                /*
                 * 處理參數(shù),先獲取參數(shù)部分存在$path
                 * 循環(huán)$path,匹配鍵值對
                 * 如果有鍵沒值,則匹配失敗,不存入?yún)?shù)
                 * */
                $this->pathinfo['module']=$path[0];
                $this->pathinfo['controller']=$path[1];
                $this->pathinfo['action']=$path[2];
                $path=array_slice($path,3);
                for($i=0;$i<count($path);$i+=2){
                    if(isset($path[$i+1])){
                        $this->params[$path[$i]]=$path[$i+1];
                    }
                }
                break;
        }


//        echo '<pre>'.print_r($this->params,true).'</pre>';

//         返回route,方便以后鏈式訪問
        return $this;
    }
    public function dispatch()
    {
        $module=$this->pathinfo['module'];
        $controller=$this->pathinfo['controller'];
        $action=$this->pathinfo['action'];

        $controller='app\\'.$module.'\controller\\'.ucfirst($controller);
//        echo $controller;
//        沒有這行代碼時,路徑出錯也會返回到首頁,所以刪除
//        if(!method_exists($controller,$action)){
//            $action=$this->action;
//            header('Location:/');
//        }
        return call_user_func_array([new $controller,$action],$this->params);
    }
}

$route=new Route($config['route']);
$path=$_SERVER['QUERY_STRING'];
$routes=$route->parse($path);
require __DIR__ . '/app/admin/controller/User.php';
echo $routes=$route->dispatch();
目錄 app/admin/controller/User.php
namespace app\admin\controller;

class User
{
    public function add()
    {
       return 'my name is '.$name.', I`m '.$age;
    }
}
訪問 127.0.0.1/index.php?admin/user/add/name/php/age/4


批改老師:韋小寶批改時間:2019-03-14 09:10:22
老師總結(jié):寫的很不錯 路由關(guān)系到整個項目的安全性 這個一定要去詳細研究哦

發(fā)佈手記

熱門詞條