自定义路由的功能,指定到pathinfo的url上,再次升级之前的脚本
simpleloader.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
<?php class simpleloader{ public static function run( $rules = array ()){ header( "content-type:text/html;charset=utf-8" ); self::register(); self::commandline(); self::router( $rules ); self:: pathinfo (); } //自动加载 public static function loadclass( $class ){ $class = str_replace ( '\\' , '/' , $class ); $dir = str_replace ( '\\' , '/' , __dir__); $class = $dir . "/" . $class . ".php" ; if (! file_exists ( $class )){ header( "http/1.1 404 not found" ); } require_once $class ; } //命令行模式 public static function commandline(){ if (php_sapi_name()== "cli" ){ $_server [ 'path_info' ]= "" ; foreach ( $_server [ 'argv' ] as $k => $v ) { if ( $k ==0) continue ; $_server [ 'path_info' ].= "/" . $v ; } } } //路由模式 public static function router( $rules ){ if (isset( $_server [ 'path_info' ]) && ! empty ( $rules )){ $pathinfo =ltrim( $_server [ 'path_info' ], "/" ); foreach ( $rules as $k => $v ) { $reg = "/" . $k . "/i" ; if (preg_match( $reg , $pathinfo )){ $res =preg_replace( $reg , $v , $pathinfo ); $_server [ 'path_info' ]= '/' . $res ; } } } } //pathinfo处理 public static function pathinfo (){ 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 ; if (method_exists( $controller , $_get [ 'a' ])){ $controller = new $class ; $controller -> $_get [ 'a' ](); } else { header( "http/1.1 404 not found" ); echo "404" ; } } //致命错误回调 public static function shutdowncallback(){ $e =error_get_last(); if (! $e ) return ; self::myerrorhandler( $e [ 'type' ], '<font color="red">fatal error</font> ' . $e [ 'message' ], $e [ 'file' ], $e [ 'line' ]); } //错误处理 protected static function myerrorhandler( $errno , $errstr , $errfile , $errline ){ list( $micseconds , $seconds )= explode ( " " ,microtime()); $micseconds = round ( $micseconds *1000); $micseconds = strlen ( $micseconds )==1 ? '0' . $micseconds : $micseconds ; if (php_sapi_name()== "cli" ){ $break = "\r\n" ; } else { $break = "<br/>" ; } $mes = "[" . date ( "y-m-d h:i:s" , $seconds ). ":{$micseconds}] " . $errfile . " " . $errline . " line " . $errstr . $break ; echo $mes ; } //注册 public static function register(){ error_reporting (0); set_error_handler( function ( $errno , $errstr , $errfile , $errline ){ self::myerrorhandler( $errno , $errstr , $errfile , $errline ); }); register_shutdown_function( function (){ self::shutdowncallback(); }); spl_autoload_register( "self::loadclass" ); } } |
如何使用
index.php
1
2
3
4
5
6
7
8
9
|
<?php //路由映射 $rules = array ( '^user$' => 'user/user/getuserlist' , '^user\/(\d+)$' => 'user/user/getuserbyid/id/$1' , '^user\/(\d+)\/article$' => 'user/user/getuserarticle/uid/$1' ); require_once "simpleloader.php" ; simpleloader::run( $rules ); |
控制器啥样
\controller\user\user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php namespace controller\user; class user{ public function getuserbyid(){ echo "用户信息id {$_get['id']} 的信息" ; } public function getuserlist(){ echo "用户列表" ; } public function getuserarticle(){ echo "用户id {$_get['uid']} 的文章列表" ; } } |
效果呢:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。