改进闭包路由的参数或者 改进路由参数的获取 支持单独获取路由

This commit is contained in:
thinkphp
2016-07-06 18:17:11 +08:00
parent 41844561be
commit 72c56548e8
2 changed files with 58 additions and 68 deletions

View File

@@ -62,7 +62,7 @@ class Request
/** /**
* @var array 当前路由信息 * @var array 当前路由信息
*/ */
protected $route = []; protected $routeInfo = [];
/** /**
* @var array 当前调度信息 * @var array 当前调度信息
@@ -81,6 +81,7 @@ class Request
protected $get = []; protected $get = [];
protected $post = []; protected $post = [];
protected $request = []; protected $request = [];
protected $route = [];
protected $put; protected $put;
protected $delete; protected $delete;
protected $session = []; protected $session = [];
@@ -620,11 +621,27 @@ class Request
$vars = []; $vars = [];
} }
// 当前请求参数和URL地址中的参数合并 // 当前请求参数和URL地址中的参数合并
$this->param = array_merge($this->get(), $vars); $this->param = array_merge($this->route(), $this->get(), $vars);
} }
return $this->input($this->param, $name, $default, $filter); return $this->input($this->param, $name, $default, $filter);
} }
/**
* 设置获取获取路由参数
* @access public
* @param string|array $name 变量名
* @param mixed $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function route($name = '', $default = null, $filter = null)
{
if (is_array($name)) {
return $this->route = array_merge($this->route, $name);
}
return $this->input($this->route, $name, $default, $filter);
}
/** /**
* 设置获取获取GET参数 * 设置获取获取GET参数
* @access public * @access public
@@ -882,25 +899,6 @@ class Request
return isset($this->header[$name]) ? $this->header[$name] : $default; return isset($this->header[$name]) ? $this->header[$name] : $default;
} }
/**
* 获取PATH_INFO
* @param string $name 数据名称
* @param string $default 默认值
* @param string|array $filter 过滤方法
* @return mixed
*/
public function pathParam($name = '', $default = null, $filter = null)
{
$pathinfo = $this->pathinfo();
if (!empty($pathinfo)) {
$depr = Config::get('pathinfo_depr');
$input = explode($depr, trim($pathinfo, $depr));
return $this->input($input, $name, $default, $filter);
} else {
return $default;
}
}
/** /**
* 获取变量 支持过滤和默认值 * 获取变量 支持过滤和默认值
* @param array $data 数据源 * @param array $data 数据源
@@ -1279,17 +1277,17 @@ class Request
} }
/** /**
* 获取当前请求的路由 * 获取当前请求的路由信息
* @access public * @access public
* @param array $route 路由名称 * @param array $route 路由名称
* @return array * @return array
*/ */
public function route($route = []) public function routeInfo($route = [])
{ {
if (!empty($route)) { if (!empty($route)) {
$this->route = $route; $this->routeInfo = $route;
} else { } else {
return $this->route; return $this->routeInfo;
} }
} }

View File

@@ -767,38 +767,28 @@ class Route
$key = $group . '/' . ltrim($key, '/'); $key = $group . '/' . ltrim($key, '/');
$result = self::checkRule($key, $route, $url, $pattern, $option); $result = self::checkRule($key, $route, $url, $pattern, $option);
if (false !== $result) { if (false !== $result) {
$request->route(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); $request->routeInfo(['rule' => $key, 'route' => $route, 'pattern' => $pattern, 'option' => $option]);
return $result; return $result;
} }
} }
if ($missGroup) { if ($missGroup) {
// 未匹配所有路由的路由规则处理 // 未匹配所有路由的路由规则处理
if ($missGroup instanceof \Closure) {
// 执行闭包
return ['type' => 'function', 'function' => $missGroup, 'params' => []];
} else {
return self::parseRule('', $missGroup, $url, []); return self::parseRule('', $missGroup, $url, []);
} }
}
} else { } else {
// 规则路由 // 规则路由
$result = self::checkRule($rule, $route, $url, $pattern, $option); $result = self::checkRule($rule, $route, $url, $pattern, $option);
if (false !== $result) { if (false !== $result) {
$request->route(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]); $request->routeInfo(['rule' => $rule, 'route' => $route, 'pattern' => $pattern, 'option' => $option]);
return $result; return $result;
} }
} }
} }
if (isset($miss)) { if (isset($miss)) {
// 未匹配所有路由的路由规则处理 // 未匹配所有路由的路由规则处理
if ($miss instanceof \Closure) {
// 执行闭包
return ['type' => 'function', 'function' => $miss, 'params' => []];
} else {
return self::parseRule('', $miss, $url, []); return self::parseRule('', $miss, $url, []);
} }
} }
}
return false; return false;
} }
@@ -1011,12 +1001,6 @@ class Route
return $result; return $result;
} }
} }
if ($route instanceof \Closure) {
// 解析路由参数
Request::instance()->param(array_merge($match, $_GET));
// 执行闭包
return ['type' => 'function', 'function' => $route, 'params' => $match];
}
return self::parseRule($rule, $route, $url, $match, $merge); return self::parseRule($rule, $route, $url, $match, $merge);
} }
} }
@@ -1175,12 +1159,10 @@ class Route
private static function parseRule($rule, $route, $pathinfo, $matches, $merge = false) private static function parseRule($rule, $route, $pathinfo, $matches, $merge = false)
{ {
// 解析路由规则 // 解析路由规则
if ($rule) {
$rule = explode('/', $rule); $rule = explode('/', $rule);
// 获取URL地址中的参数 // 获取URL地址中的参数
$paths = $merge ? explode('/', $pathinfo, count($rule)) : explode('/', $pathinfo); $paths = $merge ? explode('/', $pathinfo, count($rule)) : explode('/', $pathinfo);
// 获取路由地址规则
$url = is_array($route) ? $route[0] : $route;
foreach ($rule as $item) { foreach ($rule as $item) {
$fun = ''; $fun = '';
if (0 === strpos($item, '[:')) { if (0 === strpos($item, '[:')) {
@@ -1194,14 +1176,24 @@ class Route
array_shift($paths); array_shift($paths);
} }
} }
} else {
$paths = explode('/', $pathinfo);
}
// 获取路由地址规则
$url = is_array($route) ? $route[0] : $route;
// 替换路由地址中的变量 // 替换路由地址中的变量
if (is_string($url)) {
foreach ($matches as $key => $val) { foreach ($matches as $key => $val) {
if (false !== strpos($url, ':' . $key)) { if (false !== strpos($url, ':' . $key)) {
$url = str_replace(':' . $key, $val, $url); $url = str_replace(':' . $key, $val, $url);
unset($matches[$key]); unset($matches[$key]);
} }
} }
if (0 === strpos($url, '/') || 0 === strpos($url, 'http')) { }
if ($url instanceof \Closure) {
// 执行闭包
$result = ['type' => 'function', 'function' => $url, 'params' => $matches];
} elseif (0 === strpos($url, '/') || 0 === strpos($url, 'http')) {
// 路由到重定向地址 // 路由到重定向地址
$result = ['type' => 'redirect', 'url' => $url, 'status' => (is_array($route) && isset($route[1])) ? $route[1] : 301]; $result = ['type' => 'redirect', 'url' => $url, 'status' => (is_array($route) && isset($route[1])) ? $route[1] : 301];
} elseif (0 === strpos($url, '\\')) { } elseif (0 === strpos($url, '\\')) {
@@ -1262,7 +1254,7 @@ class Route
} }
} }
// 设置当前请求的参数 // 设置当前请求的参数
Request::instance()->param(array_merge($var, $_GET)); Request::instance()->route($var);
} }
// 分析路由规则中的变量 // 分析路由规则中的变量