URL到类方法的映射例子

Original 2019-01-24 09:34:04 372
abstract:本章主要学习了MVC的架构原理,并通过配置简单的路由,实现类到路由方法的映射,代码如下:<?php /**路由类*/ namespace pong; class Route {     protected $route=[];     protected $passIn

本章主要学习了MVC的架构原理,并通过配置简单的路由,实现类到路由方法的映射,代码如下:

<?php
/**路由类*/

namespace pong;

class Route
{
    protected $route=[];
    protected $passInfo=[];
    protected $params=[];

    public function __construct($route)
    {
        $this->route=$route;
    }

    /**
     * @param string $queryStr  链接参数
     * @return $this            当前类的实例
     */
    public  function  parse($queryStr='')
    {
        //去除字符串左右的/,并将参数全部转为小写
        $queryStr=strtolower(trim($queryStr,'/'));
        //通过/把字符串转成数组
        $queryArr=explode('/',$queryStr);
        //数组过滤掉空字符
        $queryArr=array_filter($queryArr,function($value){
            return trim($value)!='';
        },ARRAY_FILTER_USE_BOTH);

        //判断数组中有几个参数
        switch (count($queryArr))
        {
            case 0:
                $this->passInfo = $this->route;
                break;
            case 1:
                $this->passInfo['module']=$queryArr[0];
                break;
            case 2:
                $this->passInfo['module']=$queryArr[0];
                $this->passInfo['controller']=$queryArr[1];
                break;
            case 3:
                $this->passInfo['module']=$queryArr[0];
                $this->passInfo['controller']=$queryArr[1];
                $this->passInfo['action']=$queryArr[2];
                break;
            default:
                $this->passInfo['module']=$queryArr[0];
                $this->passInfo['controller']=$queryArr[1];
                $this->passInfo['action']=$queryArr[2];

                //从第四个元素开始遍历,即过滤掉路由的信息
                for($i=3;$i<count($queryArr);$i+=2)
                {
                    if(isset($queryArr[$i+1]))
                    {
                        $this->params[$queryArr[$i]]=$queryArr[$i+1];
                    }
                }
                break;
        }

        //返回当前对象
        return $this;
    }

    public function  dispatch()
    {

        //模块名
        $module = $this->passInfo['module'];
        //控制器名
        $controller='\app\\' .$module . '\controller\\' .  ucfirst($this->passInfo['controller']) ;
        //操作
        $action= $this->passInfo['action'];


        //如果找不到,重新返回根目录
        if(!method_exists($controller,$action))
        {
            header('location:/');
        }

        return call_user_func_array([new $controller,$action],$this->params);

    }
}

//测试
$config=require __DIR__.'/Config.php';
$route=new Route($config['route']);
//测试请求分发
require __DIR__ . '/../app/admin/controller/Index.php';
print_r($route->parse($_SERVER['QUERY_STRING'])->dispatch()) ;

Config.php

<?php
/**
 * 配置文件
 */

return [
    //应用配置
    'app'=>[
        //调试开关
        'debug'=>true
    ],

    //路由配置
    'route'=>[
        //默认模块
        'module'=>'admin',

        //默认控制器
        'controller'=>'Index',

        //默认操作
        'action'=>'index'
    ],

    'db'=>[
        // 数据库类型
        'database_type' => 'mysql',

        // 默认数据库名称
        'database_name' => 'frame',

        // 默认主机名
        'server' => 'localhost',

        // 默认用户名
        'username' => 'root',

        // 用户密码
        'password' => 'root',

        // 编码
        'charset' => 'utf8',

        //端口
        'port' => 3306,
    ]
];

Index.php

<?php

namespace app\admin\controller;


class Index
{
    public function hello($name,$age)
    {
        return '我是调用的方法。姓名:' . $name . ',年龄:' . $age;
    }
}

调用地址:

域名/pong/route.php?/admin/Index/Hello/name/pong/age/26

效果图:

QQ截图20190124093245.jpg

Correcting teacher:天蓬老师Correction time:2019-01-24 10:44:17
Teacher's summary:基本上是照抄的教学代码, 不知你是否真的明白了, 应该写上你的理解

Release Notes

Popular Entries