首页 > Web开发 > 详细

php 自制简单路由类 望大神指点

时间:2017-06-09 19:01:48      阅读:248      评论:0      收藏:0      [点我收藏+]
class route
{
/** @var null 模块 */
private static $module = null;
/** @var null 控制器 */
private static $controller = null;
/** @var null 方法 */
private static $action = null;
/** @var 控制器url */
private static $controllerPath;

/**
* route constructor.
* @param array $config url
*/
public function __construct($config = [])
{
$var_parse = explode(‘/‘, $config[‘r‘]);
if (count($var_parse) == 3){
self::$module = $var_parse[0];
self::$controller = ucwords($var_parse[1]).‘Controller‘;
self::$action = $var_parse[2];
self::$controllerPath = APP_ROOT.‘/controllers/‘.self::$module.‘/‘.self::$controller.‘.php‘;
}elseif(count($var_parse) == 2){
self::$controller = ucwords($var_parse[0]).‘Controller‘;
self::$action = $var_parse[1];
self::$controllerPath = APP_ROOT.‘/controllers/‘.self::$controller.‘.php‘;
}
}

/**
* 执行控制器方法
* @return null
*/
public static function run(){
self::register();
$controller = new self::$controller;
if(method_exists($controller, self::$action)){
$action = self::$action;
$controller->$action();
}else{
header("HTTP/1.1 404 Not Found");
}
}

/**
* 加载类
*/
public static function register(){
spl_autoload_register("self::loadClass");
}

//自动加载
public static function loadClass(){
if(!file_exists(self::$controllerPath)){
header("HTTP/1.1 404 Not Found");
}
require_once self::$controllerPath;
}
}

php 自制简单路由类 望大神指点

原文:http://www.cnblogs.com/jiangfb/p/6973259.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!