自定义路由的功能,指定到
pathinfo
?/p>
url
?/p>
,
再次升级之前的脚?/p>
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::pathInfo();
}
//
自动加载
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;
}
//
命令行模?/p>
public static function commandLine(){
if(php_sapi_name()=="cli"){
$_SERVER['PA
TH_INFO']="";
foreach ($_SERVER['argv'] as $k=>$v) {
if($k==0) continue;
$_SERVER['PA
TH_INFO'].="/".$v;
}
}
}
//
路由模式
public static function router($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;
}
}