[PHP] Implementing a route map to a specified controller

Source: Internet
Author: User
Tags explode

Custom routing features, specifying the URL to PathInfo, before the script is upgraded again

simpleloader.php

<?PHPclasssimpleloader{ Public Static functionRun$rules=Array()){        Header("Content-type:text/html;charset=utf-8"); Self::Register (); Self::CommandLine (); Self:: Router ($rules); Self::PathInfo(); }    //Auto Load     Public Static functionLoadClass ($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 functionCommandLine () {if(Php_sapi_name() = = "CLI"){            $_server[' Path_info ']= ' ""; foreach($_server[' argv '] as $k=$v) {                if($k==0)Continue; $_server[' Path_info '].= '/'.$v; }        }    }    //Route mode     Public Static functionRouter$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 Processing     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) {                     Case1:$_get[' m ']=Ucfirst($key);  Break;  Case2:$_get[' C ']=Ucfirst($key);  Break;  Case3:$_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 functionShutdowncallback () {$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 functionMyErrorHandler ($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 functionRegister () {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 Map $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 What kind of

\controller\user\user.php

<?phpnamespace Controller\user;classuser{ Public functionGetuserbyid () {Echo"User Information ID {$_get[' ID ']} The information "; }     Public functiongetuserlist () {Echo"User List"; }     Public functiongetuserarticle () {EchoThe user ID {$_get[' UID ']} List of articles "; }}

Effect:

[PHP] Implementing a route map to a specified controller

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.