<?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+'))
)
);<?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);
}
}
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号