自己所理解的php路由
程序员文章站
2022-05-27 12:38:35
...
1. [代码]用到的信息
<?php array( //启用路由 默认不启用 'ROUTE_ON' => false, //路由规则 'ROUTE' => array( //[0],[1]匹配后面的参数0,参数1 \d,\d依次表示[0],[1]的类型 正则 //'test-[0]-[1].html' => array('控制器', '动作', array('参数0', '参数2'), array('\d', '\d+')) ) );
2. [PHP]代码
<?php /** * Url路由 * @author 布衣 * QQ 780998900 */ class Dispatcher{ public static function start(){ //检测pathinfo参数存在且启用路由则解析路由 if($_GET['pathinfo'] && C('Epoch.ROUTE_ON') == true){ self::ParseRoute(); } //获取控制器 $Control = $_GET[C('Epoch.VAR_CONTROLLER')]; //获取动作 $Action = $_GET[C('Epoch.VAR_ACTION')]; //检查子域名部署模式 //$doamin = C('Epoch.DOMAIN'); //if(count($doamin)){ //} $Control = empty($Control) ? C('Epoch.DEFAULT_CONTROLLER') : $Control; $Action = empty($Action) ? C('Epoch.DEFAULT_ACTION') : $Action; $ControlPath = APP_PATH .'Controller/'. $Control . C('Epoch.DEFAULT_CONTROLLER_SUFFIX'); if(file_exists($ControlPath)){ require_once $ControlPath; $Controlor = $Control .'Controller'; if(class_exists($Controlor)){ $obj = new $Controlor(); if(method_exists($obj, $Action)){ $obj->$Action(); }else{ self::Show('控制器动作不存在!'); } }else{ self::Show('控制器无法加载!'); } }else{ self::Show('控制器['. $ControlPath .']不存在!'); } } /** * 编译路由 * @param string $Controller 控制器 * @param string $Action 动作 * @param string $Param 参数 * @return bool|mixed|string */ public static function Route($Controller, $Action, $Param){ $routes = C('Epoch.ROUTE'); if(is_array($routes)){ foreach($routes as $pattern => $route){ $uri = false; //必须有控制器名和动作名 if(empty($route[0]) || empty($route[1])){ continue; } //获取参数是否匹配 preg_match_all('/[\d+]/', $pattern, $match); if(count($match[0]) != count($route[2])){ continue; } //检查控制器和动作是否相同 if($route[0] == $Controller && $route[1] == $Action){ $parse = parse_url_param($Param); $flag = 1; foreach($route[2] as $key => $param){ if(! isset($parse[$param])){ $flag = 0; $uri = false; break; } //替换参数 $uri = str_replace('['. $key .']', $parse[$param], $pattern); unset($parse[$param]); } //如果匹配失败 则跳过 if($flag == 0){ continue; } //检查是否还有剩余的参数 if(!empty($parse)) { $extParam = '?'; foreach ($parse as $key => $value){ $extParam .= $key .'='. $value .'&'; } $extParam = substr($extParam, 0, -1); } //组装剩余的参数 $uri .= $extParam; if($uri !== false){ return $uri; } } } } return false; } /** * 解析路由 */ protected static function ParseRoute(){ $routes = C('Epoch.ROUTE'); $pathinfo = $_GET['pathinfo']; if(! is_array($routes)){ return; } foreach($routes as $pattern => $route){ //组装正则表达式 for($i = 0; $i < 10; $i++){ $res = isset($route[3][$i]) ? $route[3][$i] : '[A-Za-z0-9]'; $pattern = str_replace('['. $i .']', '('. $res .'+)', $pattern); } $pattern = '/^'. $pattern .'$/'; //匹配路由 if(preg_match($pattern, $pathinfo, $match)){ unset($match[0]); //写入数据 $_GET[C('Epoch.VAR_CONTROLLER')] = $route[0]; $_GET[C('Epoch.VAR_ACTION')] = $route[1]; foreach($match as $key => $value){ $_GET[$route[2][$key - 1]] = $value; } } } } public static function Show($msg){ EpochException::Show($msg); } }
上一篇: 作mysql和php开发要一些什么知识
下一篇: 帮小弟我看下这段代码