实现URL到类方法的映射

原创 2019-06-06 10:00:27 405
摘要:<?php /**  * 实现URL到类方法的映射  */ namespace pig; class Route {     //配置路由信息     protected $route = [];   &nbs
<?php
/**
 * 实现URL到类方法的映射
 */

namespace pig;

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

        //第二步:解析出$queryArr数组中的内容(模块,控制器,操作,参数)
        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数组的索引3开始,将剩余的元素全部作为参数进行处理
                $arr = array_slice($queryArr,3);
                //键值对必须成对出现,所以每次递增为2
                for($i=0;$i<count($arr);$i +=2){
                    //如果没有第二参数,则放弃
                    if(isset($arr[$i+1])){
                        $this->params[$arr[$i]] = $arr[$i+1];
                    }
                }
                break;
        }
        //返回当前路由的实例对象,主要是方便链式调用:$route->parse()->worm()->print()
        return $this;
    }
    //请求分发
    public function dispatch()
    {
        //生成的带有命名空间的控制器名称:app\模块\controller\控制器
        //类名称应该与类文件所载的绝对路径一一对应,这样才可以实现自动映射,方便自动加载
        //模块名称
        $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:/');
        }
        //将用户的请求分发到指定的控制器和对应的操作方法上
        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']);
    }

}


批改老师:查无此人批改时间:2019-06-10 09:08:40
老师总结:完成的不错。很多框架都是这样作的,学习原理,更好的理解编程。继续加油

发布手记

热门词条