本文实例为大家分享了php编写一个简单的路由类,供大家参考,具体内容如下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
<?php
namespace cmhc\\Hcrail;
class Hcrail
{
/**
* callback function
* @var callable
*/
protected static $callback ;
/**
* match string or match regexp
* @var string
*/
protected static $match ;
protected static $routeFound = false;
/**
* deal with get,post,head,put,delete,options,head
* @param $method
* @param $arguments
* @return
*/
public static function __callstatic( $method , $arguments )
{
self:: $match = str_replace ( "//" , "/" , dirname( $_SERVER [ 'PHP_SELF' ]) . '/' . $arguments [0]);
self:: $callback = $arguments [1];
self::dispatch();
return ;
}
/**
* processing ordinary route matches
* @param string $requestUri
* @return
*/
public static function normalMatch( $requestUri )
{
if (self:: $match == $requestUri ) {
self:: $routeFound = true;
call_user_func(self:: $callback );
}
return ;
}
/**
* processing regular route matches
* @param string $requestUri
* @return
*/
public static function regexpMatch( $requestUri )
{
//处理正则表达式
$regexp = self:: $match ;
preg_match( "#$regexp#" , $requestUri , $matches );
if (! empty ( $matches )) {
self:: $routeFound = true;
call_user_func(self:: $callback , $matches );
}
return ;
}
/**
* dispatch route
* @return
*/
public static function dispatch()
{
if (self:: $routeFound ) {
return ;
}
$requestUri = parse_url ( $_SERVER [ 'REQUEST_URI' ], PHP_URL_PATH);
$requestMethod = $_SERVER [ 'REQUEST_METHOD' ];
if ( strpos (self:: $match , '(' ) === false) {
self::normalMatch( $requestUri );
} else {
self::regexpMatch( $requestUri );
}
}
/**
* Determining whether the route is found
* @return boolean
*/
public static function isNotFound()
{
return !self:: $routeFound ;
}
}
|
下载地址:https://github.com/cmhc/Hcrail
希望本文所述对大家学习PHP程序设计有所帮助。
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-26 97
-
2025-05-29 47
-
2025-05-29 35
-
2025-05-29 53
-
2025-05-27 15
热门评论