From 8f594e5cb2c1364d34c48086f26aefcefd7be992 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 11 Dec 2015 13:18:02 +0800 Subject: [PATCH] =?UTF-8?q?Url=E7=B1=BB=E6=B7=BB=E5=8A=A0Route=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=A0=B9=E6=8D=AE=E8=B7=AF=E7=94=B1=E5=88=AB=E5=90=8D?= =?UTF-8?q?=E8=8E=B7=E5=8F=96URL=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/route.php | 25 ++++++++++++++++++++++++- library/think/url.php | 4 +--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/library/think/route.php b/library/think/route.php index 56f4e1b0..f625d483 100644 --- a/library/think/route.php +++ b/library/think/route.php @@ -29,7 +29,7 @@ class Route // 变量规则 private static $pattern = []; // 路由别名 用于自动生成 - public static $alias = []; + private static $alias = []; // 添加URL映射规则 public static function map($map, $route = '') @@ -629,4 +629,27 @@ class Route return $result['route']; } } + + // 根据路由别名和参数获取URL地址 + public static function getRouteUrl($name, $params = []) + { + if (!empty(self::$alias[$name])) { + $url = self::$alias[$name]; + if (is_string($params)) { + parse_str($params, $vars); + } else { + $vars = $params; + } + foreach ($vars as $key => $val) { + if (false !== strpos($url, '[:' . $key . ']')) { + $url = str_replace('[:' . $key . ']', $val, $url); + } else { + $url = str_replace(':' . $key, $val, $url); + } + } + return $url; + } else { + return null; + } + } } diff --git a/library/think/url.php b/library/think/url.php index cf5b0ec1..cdd00f38 100644 --- a/library/think/url.php +++ b/library/think/url.php @@ -99,9 +99,7 @@ class Url // 根据路由名称和参数生成URL地址 public static function route($name, $params) { - if (isset(Route::$alias[$name])) { - - } + return Route::getRouteUrl($name, $params); } // 解析URL和参数 域名