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; }}
时间: 2024-08-08 13:54:00