自定义路由的功能,指定到pathinfo的url上,再次升级之前的脚本
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;
}
//命令行模式
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;
}
}
}
//路由模式
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;
}
}
}
}
//pathinfo处理
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) {
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";
}
}
//致命错误回调
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']);
}
//错误处理
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="rn";
}else{
$break="<br/>";
}
$mes="[".date("y-m-d h:i:s",$seconds).":{$micseconds}] ".$errfile." ".$errline." line ".$errstr.$break;
echo $mes;
}
//注册
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");
}
}
如何使用
index.php
<?php //路由映射 $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);
控制器啥样
controlleruseruser.php
<?php
namespace controlleruser;
class user{
public function getuserbyid(){
echo "用户信息id {$_get['id']} 的信息";
}
public function getuserlist(){
echo "用户列表";
}
public function getuserarticle(){
echo "用户id {$_get['uid']} 的文章列表";
}
}
效果呢:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!