From 8b55500f91c8ef8c296498b9318229bc138a3db2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 9 Sep 2016 20:24:42 +0800 Subject: [PATCH] =?UTF-8?q?Route=E7=B1=BB=E5=A2=9E=E5=8A=A0bind=5Fmodel?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=94=A8=E4=BA=8E=E7=BB=91=E5=AE=9A=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Route.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/library/think/Route.php b/library/think/Route.php index 5d4a5274..7eb6d3a7 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -853,9 +853,10 @@ class Route * @param string $url URL地址 * @param string $depr URL分割符 * @param string $group 路由分组名 + * @param array $options 路由参数(分组) * @return mixed */ - private static function checkRoute($request, $rules, $url, $depr = '/', $group = '') + private static function checkRoute($request, $rules, $url, $depr = '/', $group = '', $options = []) { foreach ($rules as $key => $item) { if (true === $item) { @@ -892,7 +893,7 @@ class Route continue; } - $result = self::checkRoute($request, $rule, $url, $depr, $key); + $result = self::checkRoute($request, $rule, $url, $depr, $key, $option); if (false !== $result) { return $result; } @@ -906,6 +907,9 @@ class Route if ($group) { $rule = $group . ($rule ? '/' . ltrim($rule, '/') : ''); } + if (isset($options['bind_model']) && isset($option['bind_model'])) { + $option['bind_model'] = array_merge($options['bind_model'], $option['bind_model']); + } $result = self::checkRule($rule, $route, $url, $pattern, $option); if (false !== $result) { return $result; @@ -1327,6 +1331,32 @@ class Route } } + // 绑定模型数据 + if (isset($option['bind_model'])) { + $bind = []; + foreach ($option['bind_model'] as $key => $val) { + if ($val instanceof \Closure) { + $bind[$key] = call_user_func_array($val, [$matches]); + } else { + if (is_array($val)) { + parse_str($val[1], $fields); + $model = $val[0]; + } else { + $fields = ['id']; + $model = $val; + } + $where = []; + foreach ($fields as $field) { + if (isset($matches[$field])) { + $where[$field] = $matches[$field]; + } + } + $bind[$key] = $model::where($where)->find(); + } + } + $matches = array_merge($matches, $bind); + } + // 解析额外参数 self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches); // 记录匹配的路由信息