mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
Route类增加bind_model参数用于绑定模型
This commit is contained in:
@@ -853,9 +853,10 @@ class Route
|
|||||||
* @param string $url URL地址
|
* @param string $url URL地址
|
||||||
* @param string $depr URL分割符
|
* @param string $depr URL分割符
|
||||||
* @param string $group 路由分组名
|
* @param string $group 路由分组名
|
||||||
|
* @param array $options 路由参数(分组)
|
||||||
* @return mixed
|
* @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) {
|
foreach ($rules as $key => $item) {
|
||||||
if (true === $item) {
|
if (true === $item) {
|
||||||
@@ -892,7 +893,7 @@ class Route
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = self::checkRoute($request, $rule, $url, $depr, $key);
|
$result = self::checkRoute($request, $rule, $url, $depr, $key, $option);
|
||||||
if (false !== $result) {
|
if (false !== $result) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
@@ -906,6 +907,9 @@ class Route
|
|||||||
if ($group) {
|
if ($group) {
|
||||||
$rule = $group . ($rule ? '/' . ltrim($rule, '/') : '');
|
$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);
|
$result = self::checkRule($rule, $route, $url, $pattern, $option);
|
||||||
if (false !== $result) {
|
if (false !== $result) {
|
||||||
return $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);
|
self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches);
|
||||||
// 记录匹配的路由信息
|
// 记录匹配的路由信息
|
||||||
|
|||||||
Reference in New Issue
Block a user