Skip to content

Commit

Permalink
新增默认路由匹配规则
Browse files Browse the repository at this point in the history
  • Loading branch information
steezer committed Feb 16, 2020
1 parent 1d4195d commit c2263fe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 34 deletions.
22 changes: 12 additions & 10 deletions kernel/Library/Route.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public function bind($path, $host)
//设置默认路由常量,同时使用传统路由方式匹配模式
if ($path == env('ROOT_URL') || USE_DEFUALT_HANDLE) {
!isset($route_c) && ($route_c = defined('BIND_CONTROLLER') ?
BIND_CONTROLLER : ucfirst($this->request->input(C('var_controller', 'c'), C('default_c'))));
constant('BIND_CONTROLLER') : ucfirst($this->request->input(C('var_controller', 'c'), C('default_c'))));
!isset($route_a) && ($route_a = defined('BIND_ACTION') ?
BIND_ACTION : $this->request->input(C('var_action', 'a'), C('default_a')));
constant('BIND_ACTION') : $this->request->input(C('var_action', 'a'), C('default_a')));
}
} else if (is_callable($handle)) {
$this->setDisposer($handle);
Expand Down Expand Up @@ -250,9 +250,10 @@ private function getRoutesByHost($host, &$configs)
*
* @param string $path 路径
* @param string $pattern 匹配模式
* @return array|false 成功返回变量的键值,失败返回false
* @param array &$output 变量输出
* @return bool
*/
private function getVars($path, $pattern)
public static function getVars($path, $pattern, &$output)
{
$index = 0;
$start = 0;
Expand Down Expand Up @@ -307,12 +308,12 @@ private function getVars($path, $pattern)
$kvType = isset($kvnts[1]) ? $kvnts[1] : 's';
if ($kvType == 'd') { // 变量类型检查
if (is_numeric($value)) {
$this->params[$kvName] = $value;
$output[$kvName] = $value;
} else {
return false;
}
} else if ($value !== '') { //此处兼容首页参数
$this->params[$kvName] = $value;
$output[$kvName] = $value;
}

}
Expand Down Expand Up @@ -354,13 +355,14 @@ private function getHandle($path, $route, $handle)
$routeLen = substr_count($route, '/');
$urlLen = substr_count($path, '/');
$optCount = substr_count($route, '?');
$isMathAll = $route === '/**';

//无参数或有参数的路径匹配
if (
($method == 'ANY' || $method == env('REQUEST_METHOD')) &&
($routeLen == $urlLen || $urlLen + $optCount == $routeLen)
($isMathAll || $routeLen == $urlLen || $urlLen + $optCount == $routeLen)
) {
if (!strcasecmp($route, $path)) {
if ($isMathAll || !strcasecmp($route, $path)) {
//如果url完全匹配(不区分大小写),直接返回
return $this->dealWithhandle($handle, $querys);
} else {
Expand All @@ -376,7 +378,7 @@ private function getHandle($path, $route, $handle)
*/
if (
strpos($kv, '{') === false ||
!$this->getVars($urlArrs[$ki], $kv)
!$this->getVars($urlArrs[$ki], $kv, $this->params)
) { // 变量匹配检查
break;
}
Expand Down Expand Up @@ -419,7 +421,7 @@ private function dealWithhandle($handle, $querys)
$varName=$kv[1];
$varValue=$this->request->input($keyName);
if($varValue!==null){
$this->getVars($varValue, $varName);
$this->getVars($varValue, $varName, $this->params);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function_exists('date_default_timezone_set') && date_default_timezone_set('Etc/G
/**
* 系统当前版本号
*/
define('STEEZE_VERSION', '1.3.1');
define('STEEZE_VERSION', '1.3.2');

/**
* 系统初始化标志
Expand Down
25 changes: 2 additions & 23 deletions storage/Routes/*.steeze.cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,12 @@
$this->assign('user', $user);
$this->display('/User/info');
},

'/session' => function(\Library\Request $request, \Library\Response $response){
session('[start]');
$str='<pre>';
$str.=var_export(
[
'header' => $request->header(),
'session_id'=>session('[id]'),
'session_name'=>session_name(),
'all_status'=>[
'PHP_SESSION_DISABLED'=>PHP_SESSION_DISABLED,
'PHP_SESSION_NONE'=>PHP_SESSION_NONE,
'PHP_SESSION_ACTIVE'=>PHP_SESSION_ACTIVE
],
'current_status'=>session_status()
], true
);
$str.='</pre>';
$response->cookie(session_name(), session('[id]'));
return $str;
},
'/hello'=> 'Index/hello',
'/test'=> 'Index/test',
'convert' => [
'/{c}/{a}'=>'{c}/{a}',
'/{c}/{a}#page={page|d}'=>'{c}/{a}',
'/{c}/{a}/{user|d}#a={id|d?}'=>'{c}/{a}',
'/{m}/{c}/{a}'=>'{m}/{c}/{a}',
]
],
'/**'=> 'Index/hello'
];

0 comments on commit c2263fe

Please sign in to comment.