/** * 运行框架 */ public static function run() { try { self::init(); Hook::init(BASE_PATH); Hook::listen('appBegin'); Hook::listen('routeParseUrl', array(Config::get('REWRITE_RULE'), Config::get('REWRITE_ON'))); //default route if (!defined('APP_NAME') || !defined('CONTROLLER_NAME') || !defined('ACTION_NAME')) { Route::parseUrl(Config::get('REWRITE_RULE'), Config::get('REWRITE_ON')); } //execute action $controller = '\\app\\' . APP_NAME . '\\controller\\' . CONTROLLER_NAME . 'Controller'; $action = ACTION_NAME; if (!class_exists($controller)) { throw new \Exception("Controller '{$controller}' not found", 404); } $obj = new $controller(); if (!method_exists($obj, $action)) { throw new \Exception("Action '{$controller}::{$action}()' not found", 404); } Hook::listen('actionBefore', array($obj, $action)); $obj->{$action}(); Hook::listen('actionAfter', array($obj, $action)); } catch (\Exception $e) { Hook::listen('appError', array($e)); } Hook::listen('appEnd'); }
/** * 初始化应用 */ public static function init() { //加载应用配置 is_file(APP_CONFIG_PATH . 'config.php') and C(require APP_CONFIG_PATH . 'config.php'); is_file(APP_LANGUAGE_PATH . C('LANGUAGE') . '.php') and L(require APP_LANGUAGE_PATH . C('LANGUAGE') . '.php'); //解析路由 Route::parseUrl(); //导入钓子 Hook::import(C('HOOK')); //禁止使用模块检测 in_array(MODULE, C('DENY_MODULE')) && halt(MODULE . '模块禁止使用'); //常量定义 defined('MODULE_PATH') or define('MODULE_PATH', empty($_GET[C('VAR_GROUP')]) ? APP_PATH . MODULE . '/' : APP_PATH . $_GET[C('VAR_GROUP')] . '/' . MODULE . '/'); defined('MODULE_CONTROLLER_PATH') or define('MODULE_CONTROLLER_PATH', MODULE_PATH . 'Controller/'); defined('MODULE_MODEL_PATH') or define('MODULE_MODEL_PATH', MODULE_PATH . 'Model/'); defined('MODULE_CONFIG_PATH') or define('MODULE_CONFIG_PATH', MODULE_PATH . 'Config/'); defined('MODULE_HOOK_PATH') or define('MODULE_HOOK_PATH', MODULE_PATH . 'Hook/'); defined('MODULE_LANGUAGE_PATH') or define('MODULE_LANGUAGE_PATH', MODULE_PATH . 'Language/'); defined('MODULE_TAG_PATH') or define('MODULE_TAG_PATH', MODULE_PATH . 'Tag/'); defined('MODULE_LIB_PATH') or define('MODULE_LIB_PATH', MODULE_PATH . 'Lib/'); //应用配置 is_file(MODULE_CONFIG_PATH . 'config.php') and C(require MODULE_CONFIG_PATH . 'config.php'); is_file(MODULE_LANGUAGE_PATH . C('LANGUAGE') . '.php') and L(require MODULE_LANGUAGE_PATH . C('LANGUAGE') . '.php'); //模板目录常量 defined('MODULE_VIEW_PATH') or define('MODULE_VIEW_PATH', strstr(C('TPL_PATH'), '/') ? C('TPL_PATH') . C('TPL_STYLE') : MODULE_PATH . C('TPL_PATH') . '/' . C('TPL_STYLE')); defined('MODULE_PUBLIC_PATH') or define('MODULE_PUBLIC_PATH', MODULE_VIEW_PATH . 'Public/'); defined('CONTROLLER_VIEW_PATH') or define('CONTROLLER_VIEW_PATH', MODULE_VIEW_PATH . CONTROLLER . '/'); //网站根-Static目录 defined("__STATIC__") or define('__STATIC__', __ROOT__ . '/Static'); defined('__HDPHP_TPL__') or define('__HDPHP_TPL__', __HDPHP__ . '/Lib/Tpl'); defined("__VIEW__") or define('__VIEW__', __ROOT__ . '/' . rtrim(MODULE_VIEW_PATH, '/')); defined("__PUBLIC__") or define('__PUBLIC__', __VIEW__ . '/Public'); defined("__CONTROLLER_VIEW__") or define('__CONTROLLER_VIEW__', __VIEW__ . '/' . CONTROLLER); //来源URL define("__HISTORY__", isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : null); //=========================环境配置 date_default_timezone_set(C('DEFAULT_TIME_ZONE')); @ini_set('memory_limit', '128M'); @ini_set('register_globals', 'off'); @ini_set('magic_quotes_runtime', 0); define('NOW', $_SERVER['REQUEST_TIME']); define('NOW_MICROTIME', microtime(true)); define('REQUEST_METHOD', $_SERVER['REQUEST_METHOD']); define('IS_GET', REQUEST_METHOD == 'GET' ? true : false); define('IS_POST', REQUEST_METHOD == 'POST' ? true : false); define('IS_PUT', REQUEST_METHOD == 'PUT' ? true : false); define('IS_AJAX', ajax_request()); define('IS_DELETE', REQUEST_METHOD == 'DELETE' ? true : false); //模块导入 alias_import(C('ALIAS')); //注册自动载入函数 spl_autoload_register(array(__CLASS__, 'autoload')); set_error_handler(array(__CLASS__, 'error'), E_ALL); set_exception_handler(array(__CLASS__, 'exception')); register_shutdown_function(array(__CLASS__, 'fatalError')); HDPHP::_appAutoLoad(); }
/** * URL调度 * @access public * * @param $config * * @throws Exception */ public static function dispatch($config) { if (isset($_GET[$config['var_pathinfo']])) { // 判断URL里面是否有兼容模式参数 $_SERVER['PATH_INFO'] = $_GET[$config['var_pathinfo']]; unset($_GET[$config['var_pathinfo']]); } elseif (IS_CLI) { // CLI模式下 index.php module/controller/action/params/... $_SERVER['PATH_INFO'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : ''; } // 检测域名部署 if (!IS_CLI && !empty($config['url_domain_deploy'])) { if ($config['url_domain_rules']) { Route::domain($config['url_domain_rules']); } if ($match = Route::checkDomain()) { !defined('BIND_MODULE') && !empty($match[0]) && define('BIND_MODULE', $match[0]); !defined('BIND_CONTROLLER') && !empty($match[1]) && define('BIND_CONTROLLER', $match[1]); !defined('BIND_ACTION') && !empty($match[2]) && define('BIND_ACTION', $match[2]); } } // 监听path_info APP_HOOK && Hook::listen('path_info'); // 分析PATHINFO信息 if (!isset($_SERVER['PATH_INFO'])) { foreach ($config['pathinfo_fetch'] as $type) { if (!empty($_SERVER[$type])) { $_SERVER['PATH_INFO'] = 0 === strpos($_SERVER[$type], $_SERVER['SCRIPT_NAME']) ? substr($_SERVER[$type], strlen($_SERVER['SCRIPT_NAME'])) : $_SERVER[$type]; break; } } } // [模块,控制器,操作] $result = [null, null, null]; if (empty($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; define('__INFO__', ''); define('__EXT__', ''); } else { $_SERVER['PATH_INFO'] = trim($_SERVER['PATH_INFO'], '/'); define('__INFO__', $_SERVER['PATH_INFO']); // URL后缀 define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION))); if (__INFO__) { if ($config['url_deny_suffix'] && preg_match('/\\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { throw new Exception('url suffix deny'); } // 去除URL后缀 $_SERVER['PATH_INFO'] = preg_replace($config['url_html_suffix'] ? '/\\.(' . trim($config['url_html_suffix'], '.') . ')$/i' : '/\\.' . __EXT__ . '$/i', '', __INFO__); $depr = $config['pathinfo_depr']; // 还原劫持后真实pathinfo $path_info = (defined('BIND_MODULE') ? BIND_MODULE . $depr : '') . (defined('BIND_CONTROLLER') ? BIND_CONTROLLER . $depr : '') . (defined('BIND_ACTION') ? BIND_ACTION . $depr : '') . $_SERVER['PATH_INFO']; // 路由检测 if (!empty($config['url_route_on'])) { // 开启路由 则检测路由配置 Route::register(!empty($config['route']) ? $config['route'] : null); $result = Route::check($path_info, $depr); if (false === $result) { // 路由无效 if ($config['url_route_must']) { throw new Exception('route not define '); } else { // 继续分析URL $result = Route::parseUrl($path_info, $depr); } } } else { // 分析URL地址 $result = Route::parseUrl($path_info, $depr); } } } if (APP_MULTI_MODULE) { $module = strtolower($result[0] ?: $config['default_module']); if ($maps = $config['url_module_map']) { if (isset($maps[$module])) { // 记录当前别名 define('MODULE_ALIAS', $module); // 获取实际的项目名 $module = $maps[MODULE_ALIAS]; } elseif (array_search($module, $maps)) { // 禁止访问原始项目 $module = ''; } } // 获取模块名称 define('MODULE_NAME', defined('BIND_MODULE') ? BIND_MODULE : strip_tags($module)); // 模块初始化 if (MODULE_NAME && !in_array(MODULE_NAME, $config['deny_module_list']) && is_dir(APP_PATH . MODULE_NAME)) { APP_HOOK && Hook::listen('app_begin'); define('MODULE_PATH', APP_PATH . MODULE_NAME . DS); define('VIEW_PATH', MODULE_PATH . VIEW_LAYER . DS); // 初始化模块 self::initModule(MODULE_NAME, $config); } else { throw new Exception('module [ ' . MODULE_NAME . ' ] not exists ', 10005); } } else { define('MODULE_NAME', ''); define('MODULE_PATH', APP_PATH); define('VIEW_PATH', MODULE_PATH . VIEW_LAYER . DS); } // 获取控制器名 $controller = strip_tags(strtolower($result[1] ?: Config::get('default_controller'))); define('CONTROLLER_NAME', defined('BIND_CONTROLLER') ? BIND_CONTROLLER : $controller); // 获取操作名 $action = strip_tags(strtolower($result[2] ?: Config::get('default_action'))); define('ACTION_NAME', defined('BIND_ACTION') ? BIND_ACTION : $action); }
/** * URL路由检测(根据PATH_INFO) * @access public * @param $config * @throws Exception */ public static function route($config) { // 解析PATH_INFO self::parsePathinfo($config); if (empty($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; define('__INFO__', ''); define('__EXT__', ''); } else { $_SERVER['PATH_INFO'] = trim($_SERVER['PATH_INFO'], '/'); define('__INFO__', $_SERVER['PATH_INFO']); // URL后缀 define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION))); // 检测URL禁用后缀 if ($config['url_deny_suffix'] && preg_match('/\\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { throw new Exception('url suffix deny'); } // 去除正常的URL后缀 $_SERVER['PATH_INFO'] = preg_replace($config['url_html_suffix'] ? '/\\.(' . trim($config['url_html_suffix'], '.') . ')$/i' : '/\\.' . __EXT__ . '$/i', '', __INFO__); } $depr = $config['pathinfo_depr']; // 路由检测 if (!empty($config['url_route_on'])) { // 开启路由 if (!empty($config['route'])) { // 注册路由定义文件 Route::register($config['route']); } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($_SERVER['PATH_INFO'], $depr, $config['url_domain_deploy']); if (false === $result) { // 路由无效 if ($config['url_route_must']) { throw new Exception('route not define '); } else { // 继续分析为模块/控制器/操作/参数...方式URL $result = Route::parseUrl($_SERVER['PATH_INFO'], $depr); } } } else { // 分析URL地址 采用 模块/控制器/操作/参数... $result = Route::parseUrl($_SERVER['PATH_INFO'], $depr); } // 注册调度机制 self::dispatch($result); }
/** * URL路由检测(根据PATH_INFO) * @access public * @param array $config * @throws Exception */ public static function route(array $config) { // 解析PATH_INFO self::parsePathinfo($config); if (empty($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; define('__INFO__', ''); define('__EXT__', ''); } else { $_SERVER['PATH_INFO'] = trim($_SERVER['PATH_INFO'], '/'); define('__INFO__', $_SERVER['PATH_INFO']); // URL后缀 define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION))); // 检测URL禁用后缀 if ($config['url_deny_suffix'] && preg_match('/\\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { throw new Exception('url suffix deny'); } // 去除正常的URL后缀 $_SERVER['PATH_INFO'] = preg_replace($config['url_html_suffix'] ? '/\\.(' . trim($config['url_html_suffix'], '.') . ')$/i' : '/\\.' . __EXT__ . '$/i', '', __INFO__); } $depr = $config['pathinfo_depr']; $result = false; // 路由检测 if (APP_ROUTE_ON && !empty($config['url_route_on'])) { // 开启路由 if (!empty($config['route'])) { // 注册路由定义文件 Route::register($config['route']); } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false); if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { // 路由无效 throw new Exception('route not define '); } } if (false === $result) { // 路由无效默认分析为模块/控制器/操作/参数...方式URL $result = Route::parseUrl($_SERVER['PATH_INFO'], $depr); } //保证$_REQUEST正常取值 $_REQUEST = array_merge($_POST, $_GET, $_COOKIE); // 注册调度机制 self::dispatch($result); }
/** * URL路由检测(根据PATH_INFO) * @access public * @param $config * @throws Exception */ public static function route($config) { // 解析PATH_INFO self::parsePathinfo($config); // 检测域名部署 if (!IS_CLI && !empty($config['url_domain_deploy'])) { if ($config['url_domain_rules']) { // 注册域名路由规则 Route::domain($config['url_domain_rules']); } // 检测域名路由 if ($match = Route::checkDomain()) { !defined('BIND_MODULE') && !empty($match[0]) && define('BIND_MODULE', $match[0]); !defined('BIND_CONTROLLER') && !empty($match[1]) && define('BIND_CONTROLLER', $match[1]); !defined('BIND_ACTION') && !empty($match[2]) && define('BIND_ACTION', $match[2]); } } if (empty($_SERVER['PATH_INFO'])) { $_SERVER['PATH_INFO'] = ''; define('__INFO__', ''); define('__EXT__', ''); $result = ['type' => 'module', 'data' => [null, null, null]]; } else { $_SERVER['PATH_INFO'] = trim($_SERVER['PATH_INFO'], '/'); define('__INFO__', $_SERVER['PATH_INFO']); // URL后缀 define('__EXT__', strtolower(pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION))); // 检测URL禁用后缀 if ($config['url_deny_suffix'] && preg_match('/\\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { throw new Exception('url suffix deny'); } // 去除正常的URL后缀 $_SERVER['PATH_INFO'] = preg_replace($config['url_html_suffix'] ? '/\\.(' . trim($config['url_html_suffix'], '.') . ')$/i' : '/\\.' . __EXT__ . '$/i', '', __INFO__); $depr = $config['pathinfo_depr']; // 还原劫持后真实pathinfo $path_info = (defined('BIND_MODULE') ? BIND_MODULE . $depr : '') . (defined('BIND_CONTROLLER') ? BIND_CONTROLLER . $depr : '') . (defined('BIND_ACTION') ? BIND_ACTION . $depr : '') . $_SERVER['PATH_INFO']; // 路由检测 if (!empty($config['url_route_on'])) { // 开启路由 注册路由定义文件 Route::register(!empty($config['route']) ? $config['route'] : null); // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($path_info, $depr); if (false === $result) { // 路由无效 if ($config['url_route_must']) { throw new Exception('route not define '); } else { // 继续分析为模块/控制器/操作/参数...方式URL $result = Route::parseUrl($path_info, $depr); } } } else { // 分析URL地址 采用 模块/控制器/操作/参数... $result = Route::parseUrl($path_info, $depr); } } return $result; }
/** * 解析路径 */ public function loadRoute() { $routeObj = new Route(); $routeObj->parseUrl(); }