From 8d356c0d6ac37e8732ce1e32c25706692e128c1e Mon Sep 17 00:00:00 2001 From: molong Date: Thu, 25 Aug 2016 21:06:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E6=A0=B8=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lang/zh-cn.php | 2 +- core/library/think/Route.php | 10 +++--- core/library/think/Url.php | 59 +++++++++++++++++++-------------- core/library/think/db/Query.php | 34 ++++++++++--------- 4 files changed, 61 insertions(+), 44 deletions(-) diff --git a/core/lang/zh-cn.php b/core/lang/zh-cn.php index 2e1247f9..778aef4d 100644 --- a/core/lang/zh-cn.php +++ b/core/lang/zh-cn.php @@ -59,5 +59,5 @@ return [ 'tag error' => '模板标签错误', 'cache write error' => '缓存写入失败', 'sae mc write error' => 'SAE mc 写入错误', - 'route name not exists' => '路由命名标识不存在', + 'route name not exists' => '路由标识不存在(或参数不够)', ]; diff --git a/core/library/think/Route.php b/core/library/think/Route.php index 73b4b039..a00e56f1 100644 --- a/core/library/think/Route.php +++ b/core/library/think/Route.php @@ -138,12 +138,14 @@ class Route /** * 设置路由绑定 * @access public - * @param string $name 路由命名标识 - * @return string|array + * @param string|array $name 路由命名标识 数组表示批量设置 + * @return array */ public static function name($name = '') { - if ('' === $name) { + if (is_array($name)) { + return self::$name = $name; + } elseif ('' === $name) { return self::$name; } else { return isset(self::$name[$name]) ? self::$name[$name] : null; @@ -301,7 +303,7 @@ class Route } $vars = self::parseVar($rule); if (isset($name)) { - self::$name[$name] = [$rule, $vars, self::$domain]; + self::$name[$name][] = [$rule, $vars, self::$domain]; } if ($group) { if ('*' != $type) { diff --git a/core/library/think/Url.php b/core/library/think/Url.php index d6237eb1..ea690e76 100644 --- a/core/library/think/Url.php +++ b/core/library/think/Url.php @@ -22,11 +22,8 @@ class Url /** * URL生成 支持路由反射 - * @param string $url URL表达式, - * 格式:'[模块/控制器/操作]?参数1=值1&参数2=值2...@域名' - * @控制器/操作?参数1=值1&参数2=值2... - * \\命名空间类\\方法?参数1=值1&参数2=值2... - * @param string|array $vars 传入的参数,支持数组和字符串 + * @param string $url 路由地址 + * @param string|array $vars 参数(支持数组和字符串)a=val&b=val2... ['a'=>'val1', 'b'=>'val2'] * @param string|bool $suffix 伪静态后缀,默认为true表示获取配置值 * @param boolean|string $domain 是否显示域名 或者直接传入域名 * @return string @@ -77,10 +74,10 @@ class Url } } if (!empty($rule) && $match = self::getRuleUrl($rule, $vars)) { - // 匹配路由命名标识 快速生成 - $url = $match; - if (!empty($rule[2])) { - $domain = $rule[2]; + // 匹配路由命名标识 + $url = $match[0]; + if (!empty($match[1])) { + $domain = $match[1]; } } elseif (!empty($rule) && isset($name)) { throw new \InvalidArgumentException('route name not exists:' . $name); @@ -90,7 +87,7 @@ class Url parse_str($info['query'], $params); $vars = array_merge($params, $vars); } - // 路由不存在 直接解析 + // 路由标识不存在 直接解析 $url = self::parseUrl($url, $domain); } @@ -212,11 +209,15 @@ class Url } } } - } else { - $domain .= strpos($domain, '.') ? '' : strstr($request->host(), '.'); + } elseif (!strpos($domain, '.')) { + $rootDomain = Config::get('url_domain_root'); + if (empty($rootDomain)) { + $host = $request->host(); + $rootDomain = substr_count($host, '.') > 1 ? substr(strstr($host, '.'), 1) : $host; + } + $domain .= '.' . $rootDomain; } - $domain = ($request->isSsl() ? 'https://' : 'http://') . $domain; - return $domain; + return ($request->isSsl() ? 'https://' : 'http://') . $domain; } // 解析URL后缀 @@ -234,18 +235,28 @@ class Url // 匹配路由地址 public static function getRuleUrl($rule, &$vars = []) { - list($url, $pattern) = $rule; - foreach ($pattern as $key => $val) { - if (isset($vars[$key])) { - $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $vars[$key], $url); - unset($vars[$key]); - } elseif (2 == $val) { - $url = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url); - } else { - return false; + foreach ($rule as $item) { + list($url, $pattern, $domain) = $item; + if (empty($pattern)) { + return [$url, $domain]; + } + foreach ($pattern as $key => $val) { + if (isset($vars[$key])) { + $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $vars[$key], $url); + unset($vars[$key]); + $result = [$url, $domain]; + } elseif (2 == $val) { + $url = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url); + $result = [$url, $domain]; + } else { + break; + } + } + if (isset($result)) { + return $result; } } - return $url; + return false; } // 指定当前生成URL地址的root diff --git a/core/library/think/db/Query.php b/core/library/think/db/Query.php index 5b30a04b..f92ffb04 100644 --- a/core/library/think/db/Query.php +++ b/core/library/think/db/Query.php @@ -41,6 +41,12 @@ class Query protected $table = ''; // 当前数据表名称(不含前缀) protected $name = ''; + // 当前数据表主键 + protected $pk; + // 当前表字段类型信息 + protected $fieldType = []; + // 当前允许的字段列表 + protected $allowField = []; // 当前数据表前缀 protected $prefix = ''; // 查询参数 @@ -48,7 +54,7 @@ class Query // 参数绑定 protected $bind = []; // 数据表信息 - protected $info = []; + protected static $info = []; /** * 架构函数 @@ -727,11 +733,11 @@ class Query } if (true === $field) { // 获取全部字段 - $fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); + $fields = !empty($this->allowField) && ('' == $tableName || $this->getTable() == $tableName) ? $this->allowField : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); $field = $fields ?: ['*']; } elseif ($except) { // 字段排除 - $fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); + $fields = !empty($this->allowField) && ('' == $tableName || $this->getTable() == $tableName) ? $this->allowField : $this->getTableInfo($tableName ?: (isset($this->options['table']) ? $this->options['table'] : ''), 'fields'); $field = $fields ? array_diff($fields, $field) : $field; } if ($tableName) { @@ -1255,7 +1261,7 @@ class Query } elseif (is_string($field)) { $field = explode(',', $field); } - $this->options['allow_field'] = $field; + $this->allowField = $field; return $this; } @@ -1267,7 +1273,7 @@ class Query */ public function setFieldType($fieldType = []) { - $this->options['field_type'] = $fieldType; + $this->fieldType = $fieldType; return $this; } @@ -1279,7 +1285,7 @@ class Query */ public function pk($pk) { - $this->options['pk'] = $pk; + $this->pk = $pk; return $this; } @@ -1356,7 +1362,7 @@ class Query } $guid = $tableName; - if (!isset($this->info[$guid])) { + if (!isset(self::$info[$guid])) { $info = $this->connection->getFields($tableName); $fields = array_keys($info); $bind = $type = []; @@ -1374,9 +1380,9 @@ class Query } else { $pk = null; } - $this->info[$guid] = ['fields' => $fields, 'type' => $type, 'bind' => $bind, 'pk' => $pk]; + self::$info[$guid] = ['fields' => $fields, 'type' => $type, 'bind' => $bind, 'pk' => $pk]; } - return $fetch ? $this->info[$guid][$fetch] : $this->info[$guid]; + return $fetch ? self::$info[$guid][$fetch] : self::$info[$guid]; } /** @@ -1387,10 +1393,8 @@ class Query */ public function getPk($options = '') { - if (!empty($options['pk'])) { - $pk = $options['pk']; - } elseif (isset($this->options['pk'])) { - $pk = $this->options['pk']; + if (!empty($this->pk)) { + $pk = $this->pk; } else { $pk = $this->getTableInfo(is_array($options) ? $options['table'] : $options, 'pk'); } @@ -1400,13 +1404,13 @@ class Query // 获取当前数据表字段信息 public function getTableFields($options) { - return !empty($options['allow_field']) ? $options['allow_field'] : $this->getTableInfo($options['table'], 'fields'); + return !empty($this->allowField) ? $this->allowField : $this->getTableInfo($options['table'], 'fields'); } // 获取当前数据表字段类型 public function getFieldsType($options) { - return !empty($options['field_type']) ? $options['field_type'] : $this->getTableInfo($options['table'], 'type'); + return !empty($this->fieldType) ? $this->fieldType : $this->getTableInfo($options['table'], 'type'); } // 获取当前数据表绑定信息