PHP implementation route map to specified controller _php tips

Source: Internet
Author: User

Customize the routing function, specify the URL to PathInfo, and upgrade the script before

simpleloader.php

<?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::p athinfo ();
 ///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; }}//Routing mode public static function router ($rules) {if isset ($_server[' path_info ']) &&!empty ($rules)) {$pa
  Thinfo=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 handles public static function PathInfo () {if (Isset ($_server[' path_info ')) {$pathinfo =array_filter (E
  Xplode ("/", $_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
//Routing mapping
$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

<?php
namespace Controller\user;

Class user{public
 function Getuserbyid () {
 echo user information id {$_get[' id '} information ';
 }
 Public Function Getuserlist () {
 echo "user list";
 }
 List of articles for public function getuserarticle () {
 echo User ID {$_get[' uid '}}
 

Effect:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.