diff --git a/library/think/Request.php b/library/think/Request.php index 355e84fd..4bc5d61d 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -62,7 +62,7 @@ class Request /** * @var array 当前路由信息 */ - protected $route = []; + protected $routeInfo = []; /** * @var array 当前调度信息 @@ -81,6 +81,7 @@ class Request protected $get = []; protected $post = []; protected $request = []; + protected $route = []; protected $put; protected $delete; protected $session = []; @@ -620,11 +621,27 @@ class Request $vars = []; } // 当前请求参数和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); } + /** + * 设置获取获取路由参数 + * @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参数 * @access public @@ -882,25 +899,6 @@ class Request 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 数据源 @@ -1279,17 +1277,17 @@ class Request } /** - * 获取当前请求的路由 + * 获取当前请求的路由信息 * @access public * @param array $route 路由名称 * @return array */ - public function route($route = []) + public function routeInfo($route = []) { if (!empty($route)) { - $this->route = $route; + $this->routeInfo = $route; } else { - return $this->route; + return $this->routeInfo; } } diff --git a/library/think/Route.php b/library/think/Route.php index f0105842..5581716d 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -767,36 +767,26 @@ class Route $key = $group . '/' . ltrim($key, '/'); $result = self::checkRule($key, $route, $url, $pattern, $option); 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; } } if ($missGroup) { // 未匹配所有路由的路由规则处理 - if ($missGroup instanceof \Closure) { - // 执行闭包 - return ['type' => 'function', 'function' => $missGroup, 'params' => []]; - } else { - return self::parseRule('', $missGroup, $url, []); - } + return self::parseRule('', $missGroup, $url, []); } } else { // 规则路由 $result = self::checkRule($rule, $route, $url, $pattern, $option); 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; } } } 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; @@ -1011,12 +1001,6 @@ class Route 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); } } @@ -1175,33 +1159,41 @@ class Route private static function parseRule($rule, $route, $pathinfo, $matches, $merge = false) { // 解析路由规则 - $rule = explode('/', $rule); - // 获取URL地址中的参数 - $paths = $merge ? explode('/', $pathinfo, count($rule)) : explode('/', $pathinfo); + if ($rule) { + $rule = explode('/', $rule); + // 获取URL地址中的参数 + $paths = $merge ? explode('/', $pathinfo, count($rule)) : explode('/', $pathinfo); + foreach ($rule as $item) { + $fun = ''; + if (0 === strpos($item, '[:')) { + $item = substr($item, 1, -1); + } + if (0 === strpos($item, ':')) { + $var = substr($item, 1); + $matches[$var] = array_shift($paths); + } else { + // 过滤URL中的静态变量 + array_shift($paths); + } + } + } else { + $paths = explode('/', $pathinfo); + } // 获取路由地址规则 $url = is_array($route) ? $route[0] : $route; - - foreach ($rule as $item) { - $fun = ''; - if (0 === strpos($item, '[:')) { - $item = substr($item, 1, -1); - } - if (0 === strpos($item, ':')) { - $var = substr($item, 1); - $matches[$var] = array_shift($paths); - } else { - // 过滤URL中的静态变量 - array_shift($paths); - } - } // 替换路由地址中的变量 - foreach ($matches as $key => $val) { - if (false !== strpos($url, ':' . $key)) { - $url = str_replace(':' . $key, $val, $url); - unset($matches[$key]); + if (is_string($url)) { + foreach ($matches as $key => $val) { + if (false !== strpos($url, ':' . $key)) { + $url = str_replace(':' . $key, $val, $url); + 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]; } elseif (0 === strpos($url, '\\')) { @@ -1262,7 +1254,7 @@ class Route } } // 设置当前请求的参数 - Request::instance()->param(array_merge($var, $_GET)); + Request::instance()->route($var); } // 分析路由规则中的变量