PHP implements route ing to the specified controller and php route ing Controller
The custom routing function is specified to the url of pathinfo and the previous script is upgraded again.
SimpleLoader. php
<? Phpclass SimpleLoader {public static function run ($ rules = array () {header ("content-type: text/html; charset = UTF-8"); self: register (); self: commandLine (); self: router ($ rules); self: pathInfo ();} // automatically load 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;} // command line mode 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) in routing mode {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 ";}} // fatal error callback 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']);} // error handling 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;} // register 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 ");}}
How to Use
Index. php
<? Php // route ing $ 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 );
What is a controller?
\ Controller \ User. php
<? Phpnamespace Controller \ User; class User {public function getUserById () {echo "User information id {$ _ GET ['id']}";} public function getUserList () {echo "User List";} public function getUserArticle () {echo "User ID {$ _ GET ['uid']}'s article list ";}}
Effect:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.