php 框架 路由解析,来!狂撸一款PHP现代化框架 (路由的设计)

60d797c7800e324b0c707d1cad82fb09.png

前言

上一篇的标题改了一下,以一、二、三为章节对读者来说是种困扰,现在的标题是依照项目进度来编写的。上篇文章地址为 https://segmentfault.com/a/11...

这一系列文章并不准备写太多章节,大概规划的只有4~5章左右,具体实现代码还请移步Github

https://github.com/CrazyCodes...

本章详细讲解一下Route(路由的实现),Come on Up Image

f66f1029f713a3898ee83d441e92cb86.png

上图大概说明了实现路由要经过两个步骤

将所有路由信息存储到超全局变量中

用户请求时从全局变量中查找路由映射的服务脚本并实例化

OK,大概流程就是酱紫,下面开始“撸”

目录

路由的代码暂分为以下几个文件(这并不是确定的,详细可查看Github)

文件名

注释

Route

转发文件:为实现 Route::get 效果

RouteCollection

路由信息处理存储

RouteInterface

无需解释

RouteModel

路由模型,将每个路由信息以结构体方式存储到$_SERVER

Router

路由的核心类

莫急,我们一个一个文件来看。先从RouteInterface开始

RouteInterface

参照RESTful规定设定接口方法分别为 GET、POST、PATCH、PUT、DELETE、OPTIONS,当然Laravel也是规范了以上标准请求。

interface RouteInterface

{

/**

* @param $uri

* @param null $action

*

* @return mixed

*/

public function get($uri, $action = null);

/**

* @param $uri

* @param null $action

*

* @return mixed

*/

public function post($uri, $action = null);

/**

* @param $uri

* @param null $action

*

* @return mixed

*/

public function patch($uri, $action = null);

/**

* @param $uri

* @param null $action

*

* @return mixed

*/

public function put($uri, $action = null);

/**

* @param $uri

* @param null $action

*

* @return mixed

*/

public function delete($uri, $action = null);

/**

* @param $uri

* @param null $action

*

* @return mixed

*/

public function options($uri, $action = null);

}

Router

先写一个栗子

public function get($uri, $action = null)

{

return $this->addRoute("GET", $uri, $action);

}

用户调用下方代码会指向上述方法,方法既调用addRoute方法将路由信息存储到$_SERVER中

Route::get('/','Controller')

以下为addRoute部分的代码

public function addRoute($methods, $uri, $action)

{

// 这里判断请求方式是否合规,既是否存在 GET、POST、PATCH、PUT、DELETE、OPTIONS其中之一

if ($this->verify($methods) == false) {

return false;

}

// 之后我们去往RouteCollection路由信息的处理类中

return $this->routes->add($uri, $this->createRoute($methods, $action));

}

RouteCollection

最终达到 add 方法,将路由信息存储到$_SERVER中

public function add($uri, RouteModel $model)

{

if (empty($_SERVER["routes"][$uri])) {

$_SERVER["routes"][$uri] = $model;

}

}

第二个参数RouteModel开始我们说过这是路由模型,将每个路由以结构体的方式存储到变量中,存储后的结果

'routes' =>

array(6) {

'test/get' =>

class Zero\Routing\RouteModel#13 (2) {

public $method =>

string(3) "GET"

public $action =>

string(19) "testController@test"

}

'test/post' =>

class Zero\Routing\RouteModel#14 (2) {

public $method =>

string(4) "POST"

public $action =>

string(19) "testController@test"

}

'test/put' =>

class Zero\Routing\RouteModel#15 (2) {

public $method =>

string(3) "PUT"

public $action =>

string(18) "testController@put"

}

'test/del' =>

class Zero\Routing\RouteModel#16 (2) {

public $method =>

string(6) "DELETE"

public $action =>

string(18) "testController@del"

}

'test/patch' =>

class Zero\Routing\RouteModel#17 (2) {

public $method =>

string(5) "PATCH"

public $action =>

string(20) "testController@patch"

}

'test/opt' =>

class Zero\Routing\RouteModel#18 (2) {

public $method =>

string(7) "OPTIONS"

public $action =>

string(18) "testController@opt"

}

}

Route

最后通过__callStatic将代码重定向到核心类中

public static function __callStatic($name, $arguments)

{

$router = new Router;

return $router->{$name}($arguments[0], $arguments[1]);

}

上述套路部分是Laravel的设计思想,通过这款简单的框架可对Laravel核心设计有丁点的理解。

测试

测试上次做的有点糙,从本章到系列结束,我们都以PHPunit来测试。

/**

* @content tests all methods storage -> $_SERVER["routes"]

*/

public function testAllMethodsStorage()

{

$this->routes->get($methodGet = "test/get", "testController@test");

$this->assertArrayHasKey($methodGet, $_SERVER[$this->methodsDataKey]);

$this->routes->post($methodPost = "test/post", "testController@test");

$this->assertArrayHasKey($methodPost, $_SERVER[$this->methodsDataKey]);

$this->routes->put($methodPut = "test/put", "testController@put");

$this->assertArrayHasKey($methodPut, $_SERVER[$this->methodsDataKey]);

$this->routes->delete($methodDel = "test/del", "testController@del");

$this->assertArrayHasKey($methodDel, $_SERVER[$this->methodsDataKey]);

$this->routes->patch($methodPatch = "test/patch", "testController@patch");

$this->assertArrayHasKey($methodPatch, $_SERVER[$this->methodsDataKey]);

$this->routes->options($methodOpt = "test/opt", "testController@opt");

$this->assertArrayHasKey($methodOpt, $_SERVER[$this->methodsDataKey]);

}

上述贴出部分代码,以过程化的方法去测试。查看存储是否符合预期。

/**

* @content RouteModel Success

*/

public function testCreateRoute()

{

$response = $this->routes->createRoute("GET", "TestController@Get");

$this->assertInstanceOf(RouteModel::class, $response);

}

包括测试对路由创建后是否为RouteModel的实现。具体可查看Github

https://github.com/CrazyCodes...

致谢

上述已完成了路由的基本设计,下一章将讲解从启动到请求路由映射到服务脚本的过程。

希望本章可以帮到你,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TP(ThinkPHP框架路由解析原理可以简单概括为以下三个步骤: 1. 获取请求信息:TP框架通过PHP的超全局变量`$_SERVER`和`$_REQUEST`获取当前请求的URL、请求方法等信息。 2. 解析路由规则:TP框架会根据路由规则解析当前请求的控制器、方法以及传递给方法的参数。路由规则可以通过配置文件或者注解方式定义。 3. 执行控制器方法:TP框架根据解析出来的控制器、方法和参数,执行相应的业务逻辑。 具体来说,TP框架路由解析过程如下: 1. 获取请求信息: TP框架通过`$_SERVER`变量获取当前请求的URL和请求方法,例如: ``` $requestUri = $_SERVER['REQUEST_URI']; // 获取请求的URL $requestMethod = $_SERVER['REQUEST_METHOD']; // 获取请求的方法 ``` 2. 解析路由规则: TP框架支持多种路由规则,例如: - URL模式:`/:controller/:action/:id`,解析后会得到控制器名、方法名和参数`id`; - 路由规则:`'user/:id' => 'index/user/read'`,将`user/123`请求解析为`Index`控制器的`user`方法,并传递参数`id=123`; - 注解方式:使用注解标记控制器和方法,例如: ```php /** * @route('hello/:name') */ public function hello($name) { echo 'Hello '.$name; } ``` 以上是TP框架路由的基本使用方法,具体可以参考官方文档。 3. 执行控制器方法: TP框架根据解析出来的控制器名、方法名和参数,执行相应的业务逻辑。例如: ```php // 根据控制器名和方法名,实例化控制器对象并调用方法 $controller = '\\app\\index\\controller\\'.$controllerName; $instance = new $controller(); $instance->$actionName($param); ``` 注意,TP框架还支持路由缓存等优化方式,可以提高路由解析效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值