A simple php route class and a simple php route class
The example in this article shows you how to compile a simple routing class in php for your reference. The specific content is as follows:
<? Phpnamespace 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 regul Ar route matches * @ param string $ requestUri * @ return */public static function regexpMatch ($ requestUri) {// process regular expressions $ 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 {se Lf: regexpMatch ($ requestUri) ;}/ *** Determining whether the route is found * @ return boolean */public static function isNotFound () {return! Self: $ routeFound ;}}
: Https://github.com/cmhc/Hcrail
I hope this article will help you learn PHP programming.