使用自动加载和解析url的参数,实现调用到不同的控制器,实现了pathinfo模式和普通的url模式
文件结构:
|–Controller
|–Index
|–Index.php
|–Application.php
Application.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
|
<?php
class Application{
public static function main(){
header("content-type:text/html;charset=utf-8");
self::register();
self::router();
}
public static function register(){
spl_autoload_register("self::loadClass");
}
public static function loadClass($class){
$class=str_replace('\\\\', '/', $class);
$class="./".$class.".php";
require_once $class;
}
public static function router(){
if(isset($_SERVER['PATH_INFO'])){
$pathinfo=array_filter(explode("/", $_SERVER['PATH_INFO']));
for($i=1;$i<=count($pathinfo);$i++){
$key=isset($pathinfo[$i]) ? $pathinfo[$i] : '';
$value=isset($pathinfo[$i+1]) ? $pathinfo[$i+1] :"";
switch ($i) {
case 1:
$_GET['m']=ucfirst($key);
break;
case 2:
$_GET['c']=ucfirst($key);
break;
case 3:
$_GET['a']=$key;
break;
default:
if($i>3){
if($i%2==0){
$_GET[$key]=$value;
}
}
break;
}
}
}
$_GET['m']=!empty($_GET['m']) ? ucfirst($_GET['m']) : 'Index';
$_GET['c']=!empty($_GET['c']) ? ucfirst($_GET['c']) : 'Index';
$_GET['a']=!empty($_GET['a']) ? $_GET['a'] : 'index';
$class="\\\\Controller\\\\{$_GET['m']}\\\\{$_GET['c']}";
$controller=new $class;
$controller->$_GET['a']();
}
}
Application::main();
|
\\Controller\\Index\\Index.php
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
namespace Controller\\Index;
use Service\\User;
class Index{
public function __construct(){
echo "构造方法<br/>";
}
public function index(){
new User();
print_r($_GET);
}
public function login(){
echo "login()";
}
}
|
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 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交流群
您的支持,是我们最大的动力!
热门文章
-
thinkphp3.2嵌入百度编辑器ueditor的实例代码
2025-05-29 117 -
解决mybatisPlus 中的field-strategy配置失效问题
2025-05-29 35 -
2025-05-29 99
-
2025-06-04 42
-
2025-06-04 86
热门评论


