本文实例讲述了PHP从零开始打造自己的MVC框架之路由类实现方法。分享给大家供大家参考,具体如下:
在core目录下,新建一个名为lib的子目录,然后把我们前面写个route.php这个文件移动到这个目录下。
因为route类文件路径修改,所以在实例化的时候:
?
1
|
new \\core\\lib\\route();
|
然后我们来完善route.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
|
<?php
namespace core\\lib;
class Route
{
public $controller ; // 控制器
public $action ; // 方法(动作)
public function __construct()
{
// xxx.com/index.php/index/index
// xxx.com/index.php/index
/*
* 1.隐藏index.php
* 2.获取URL 参数部分
* 3.返回对应控制器和方法
* */
if (isset( $_SERVER [ 'REQUEST_URI' ]) && $_SERVER [ 'REQUEST_URI' ] != '/' ){
// 处理成这种格式:index/index
$path = $_SERVER [ 'REQUEST_URI' ];
$pathArr = explode ( '/' ,trim( $path , '/' ));
if (isset( $pathArr [0])){
$this ->controller = $pathArr [0];
}
unset( $pathArr [0]);
if (isset( $pathArr [1])){
$this ->action = $pathArr [1];
unset( $pathArr [1]);
} else {
$this ->action = 'index' ;
}
// url多余部分(参数部分)转换成 GET
// id/1/str/2
$count = count ( $pathArr ) + 2;
$i = 2;
while ( $i < $count ){
if (isset( $pathArr [ $i + 1])){
$_GET [ $pathArr [ $i ]] == $pathArr [ $i + 1];
}
$i = $i + 2;
}
p( $_GET ); // 打印GET
} else {
$this ->controller = 'index' ; // 默认控制器
$this ->action = 'index' ; // 默认方法
}
}
}
|
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/github_26672553/article/details/53884648
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 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-25 91 -
2025-05-27 88
-
2025-05-29 80
-
2025-05-27 37
-
2025-05-29 22
热门评论