编程爱好者之家
有的时候不想再nginx或者apache里面配置路由规则,那么就需要在框架内做伪静态,下面小编就简介下具体的实现方法。
具体思路是 解析前台的路由,然后根据正则匹配找到对应的框架内的方法,然后传递参数。
$rules = array( "/test/(\d+)\.html"=>'c=test&a=info&ID=$1', "/mytest/"=>'c=test&a=testaction', );
//路由规则 require_once APP.'/myroute.php'; $url = $_SERVER['REQUEST_URI']; $method = $_SERVER['REQUEST_METHOD']; if(!empty($rules)){ foreach($rules as $key=>$value){ $isMatched = preg_match('#^'.$key.'#is', $url, $matches); if(!empty($matches)){ parse_str($value,$newarr) ; $controller = $newarr['c']; $action = $newarr['a']; unset($newarr['c']); unset($newarr['a']); unset($matches[0]); $i = 1; foreach($newarr as $k=>$v){ if($method=='POST'){ $_POST[$k] = $matches[$i]; }else{ $_GET[$k] = $matches[$i]; } $i++; } } } } # 加载控制器 $controllerName = $controller.'Controller'; if (!class_exists($controllerName)){ Header("HTTP/1.1 404 Not Found"); Header("status: 404 not found"); include(ROOT."/404.html"); exit; }else{ $controllerClass = new $controllerName(); $controllerClass->$action(); }
以上就是编程爱好者之家为大家带来的PHP设置伪静态的具体方法