这里我们定义一个app类作为项目的基本对象当前类中定义了两个方法appInit()初始化项目并返回类对象run() 根据对应的配置运行指定的路由和操作为方便处理我这里直接遍历了controller目录,在实际的项目中应该使用自动加载机制按需加载初始化时将controller目录下的全部控制器方法调用route类加入到路由列表 到这里我们只剩最后一步了 , 将请求过来的URL解析,找到controller 和 action 然后执行方法这一步我们交给run()方法执行,run()方法接收$_GET参数 c 和 a 分别对应 控制器和动作判断如果路由列表中有对应的控制器和方法则将类实例化 , 调用对应方法,如果路由列表中没有这个对应路由则返回404错误 。需要指出的是这里404并不一定是没有这个对应方法,也有可能是有方法但没有使用route注解修饰导致反射找不到这个方法 。

文章插图
有此控制器和动作执行对应类对应方法

文章插图
没有这个动作或控制器返回404最后为了健壮性我们增加一个默认的控制器和默认动作,如果URL中没有传任何控制器和动作参数则走默认动作,将这个配置提取到配置文件中<?php$config = array('default_controller'=>'index','default_function'=>'index',);return $config;这样如果什么都不传我们的代码会默认访问index控制器下的index方法

文章插图
贴上完整代码:<?php#[Attribute(Attribute::IS_REPEATABLE|Attribute::TARGET_METHOD)]class route{public static $all = [];public static $path = '';public static $method = 'GET';public static $function = '';public static $controller = '';public function __construct(){}public function setPath(string $path):self{$this->path = $path;return $this;}public function setMethod(string $method):self{$this->method = $method;return $this;}public function setFunction(string $function):self{$this->function = $function;return $this;}public function setController(string $controller):self{$this->controller = $controller;return $this;}public function addRoute():void{self::$all[str_replace("Controller","",$this->controller)][$this->function] = $this;}public static function setRoute($controllerClass){$ref = new ReflectionClass($controllerClass);$controller = $ref->getName();$methods = $ref->getMethods();foreach($methods as $method){$function = $method->getName();$attributes = $method->getAttributes(route::class);foreach($attributes as $attribute){$route = $attribute->newInstance();// 拿到注解上的参数$params = $attribute->getArguments();$route->setController($controller)->setFunction($function)->setPath($params[0])->setMethod($params[1])->addRoute();}}}}route.php <?phpclass app{public function appInit():self{if(glob("./controller/*.php")){foreach(glob("./controller/*.php") as $fileName){require_once($fileName);$className = str_replace("./controller/","",str_replace(".php","",$fileName));route::setRoute($className);//route::$all;exit();}}return $this;}public function run($config){$controller = $_GET['c']??$config['default_controller'];$action = $_GET['a']??$config['default_function'];if(isset(route::$all[$controller][$action])){$route = route::$all[$controller][$action];$className = $route->controller;$function = $route->function;(new $className)->$function();}else{exit("404 Not Found!");}}}init.php <?phpclass indexController{#[route('/controller/index','get')]public function index():void{echo "This is attribute index controller \r\n";}#[route('/controller/test','get')]public function test():void{echo "This is attribute test controller \r\n";}}indexController.php <?phpclass workController{#[route('/controller/work','post')]public function work():void{echo "This is attribute work controller \r\n";}}workController.php <?php$config = array('default_controller'=>'index','default_function'=>'index',);return $config;config.php <?phprequire_once('./config.php');require_once('./route.php');require_once('./init.php');(new app)->appInit()->run($config);index.php
推荐阅读
- netty系列之: 在netty中使用 tls 协议请求 DNS 服务器
- 使用LabVIEW实现基于pytorch的DeepLabv3图像语义分割
- 天猫精灵cc7怎么使用_天猫精灵cc7使用方法
- iPadmini6使用体验_iPadmini6使用感受
- Nginx 使用自签名证书实现 https 反代 Spring Boot 中碰到的页面跳转问题
- 真我v15开箱_真我x50实际使用测评
- 如何使用微信截图(微信怎么截图发给别人)
- 华为mate40手机怎么截屏_华为mate40截图功能怎么使用
- 当贝智慧盒子Z1 Pro值得买吗_当贝智慧盒子Z1 Pro使用评价
- disk磁盘分区软件使用教程,磁盘扩容无损备份