路由类(路由解析与请求分发)

原创 2018-12-25 16:59:34 388
摘要:<?php /**  * 路由解析类(路由解析,请求分发)  */ namespace pig; class Route {     //路由信息     protected $route = [];   &n
<?php
/**
 * 路由解析类(路由解析,请求分发)
 */

namespace pig;

class Route
{
    //路由信息
    protected $route = [];
    //PATHINFO
    private $pathinfo = [];
    //url参数
    protected $params = [];

    public function __construct($route)
    {
        //初始化路由配置
        $this->route = $route;
    }

    /**
     * 解析路由
     * @param string $queryStr
     * @return $this
     */
    public function parse($queryStr = '')
    {
        //      /admin/user/add/name=peter/age=30
        //      $this->pathinfo = ['module'=>'admin','controller'=>'user','action'=>'add']
        //      参数数组:$this=>params = ['name'=>'peter','age'=>'30']
        //1、去掉查询字符串前后的/,再按/分割到数组中
        $queryStr = trim(strtolower($queryStr), '/');//去除前后的/
        $queryArr = explode('/', $queryStr);//分割字符串,并存到数组中
//        $queryArr = array_filter($queryArr);//过滤掉空字符
        //2、解析查询数组中的内容
        switch (count($queryArr)) {
            case 0:
                //使用默认的模块、控制器、方法
                $this->pathinfo = $this->route;
                break;
            case 1:
                //自定义模块,控制器和方法使用默认
                $this->pathinfo['module'] = $queryArr[0];
                break;
            case 2:
                //自定义模块、控制器,方法使用默认
                $this->pathinfo['module'] = $queryArr[0];
                $this->pathinfo['controller'] = $queryArr[1];
                break;
            case 3:
                //模块、控制器、方法都自定义
                $this->pathinfo['module'] = $queryArr[0];
                $this->pathinfo['controller'] = $queryArr[1];
                $this->pathinfo['action'] = $queryArr[2];
                break;
            default:
                //对参数进行处理
                $this->pathinfo['module'] = $queryArr[0];
                $this->pathinfo['controller'] = $queryArr[1];
                $this->pathinfo['action'] = $queryArr[2];

                //$pathinfo数组中从第4个参数开始当作参数处理
                $arr = array_slice($queryArr, 3);//从索引为3开始,取出数组剩余元素
                for ($i = 0; $i < count($arr); $i += 2) {
                    //如果没有第二个参数,则放弃
                    if (isset($arr[$i + 1])) {
                        $this->params[$arr[$i]] = $arr[$i + 1];
                    }
                }
                break;
        }
        return $this;
    }

    /**
     * 请求分发
     */
    public function dispath()
    {
       //生成带有命名空间的控制器类名称
        //类名称应该与类文件所在的绝对路径一一对应,这样可以实现自动映射,实现自动加载
        $module = $this->pathinfo['module'];
        $controller = 'app\\'.$module.'\controller\\'.ucfirst($this->pathinfo['controller']);
        $action = $this->pathinfo['action'];
        //检查controller类中的action方法是否存在,不存在,路由重定向到首页
        if(!method_exists($controller,$action)){
            $action = $this->route['action'];
            header('Location:/');
        }
        //请求分发
        return call_user_func_array([new $controller,$action],$this->params);

    }

}


批改老师:韦小宝批改时间:2018-12-25 17:37:01
老师总结:写的很不错啊!课后记得要多总结和练习哦!

发布手记

热门词条