A simple php route class and a simple php route class

Source: Internet
Author: User
Tags learn php programming

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.