改进Request的param方法和Route类的parseUrlParams方法

This commit is contained in:
thinkphp
2016-06-16 13:39:55 +08:00
parent 3fd9f0bc13
commit a7df3d52e2
2 changed files with 15 additions and 8 deletions

View File

@@ -173,23 +173,23 @@ class Request
$info['path'] = '/'; $info['path'] = '/';
} }
$options = []; $options = [];
$options['param'] = $params;
$queryString = ''; $queryString = '';
if (isset($info['query'])) { if (isset($info['query'])) {
parse_str(html_entity_decode($info['query']), $query); parse_str(html_entity_decode($info['query']), $query);
if (!empty($options['param'])) { if (!empty($params)) {
$options['param'] = array_replace($query, $options['param']); $params = array_replace($query, $params);
$queryString = http_build_query($query, '', '&'); $queryString = http_build_query($query, '', '&');
} else { } else {
$options['param'] = $query; $params = $query;
$queryString = $info['query']; $queryString = $info['query'];
} }
} elseif (isset($options['param'])) { } elseif (!empty($params)) {
$queryString = http_build_query($options['param'], '', '&'); $queryString = http_build_query($params, '', '&');
} }
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : ''); $server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
$server['QUERY_STRING'] = $queryString; $server['QUERY_STRING'] = $queryString;
$options['cookie'] = $cookie; $options['cookie'] = $cookie;
$options['param'] = $params;
$options['file'] = $files; $options['file'] = $files;
$options['server'] = $server; $options['server'] = $server;
$options['url'] = $server['REQUEST_URI']; $options['url'] = $server['REQUEST_URI'];
@@ -554,6 +554,11 @@ class Request
*/ */
public function param($name = '', $default = null) public function param($name = '', $default = null)
{ {
if(is_array($name)){
// 设置param
$this->param = array_merge($this->param, $name);
return;
}
if (empty($this->param)) { if (empty($this->param)) {
$method = $this->method(true); $method = $this->method(true);
// 自动获取请求变量 // 自动获取请求变量

View File

@@ -1178,7 +1178,7 @@ class Route
} }
/** /**
* 解析URL地址中的参数到$_GET * 解析URL地址中的参数Request对象
* @access private * @access private
* @param string $rule 路由规则 * @param string $rule 路由规则
* @param array $var 变量 * @param array $var 变量
@@ -1195,7 +1195,9 @@ class Route
}, $url); }, $url);
} }
} }
$_GET = array_merge($var, $_GET);
// 设置当前请求的参数
Request::instance()->param(array_merge($var, $_GET));
} }
} }