路由到class 改为 路由到 callable 适用于更多的回调机制

This commit is contained in:
thinkphp
2016-01-10 08:52:11 +08:00
parent bac655013c
commit 56cffea817
2 changed files with 9 additions and 11 deletions

View File

@@ -97,9 +97,9 @@ class App
// 执行控制器操作 // 执行控制器操作
$data = Loader::action(self::$dispatch['controller'], self::$dispatch['params']); $data = Loader::action(self::$dispatch['controller'], self::$dispatch['params']);
break; break;
case 'class': case 'callable':
// 执行类,例如行为 // 执行回调方法
$data = Hook::exec(self::$dispatch['class'], self::$dispatch['method'], self::$dispatch['params']); $data = call_user_func_array(self::$dispatch['callable'], self::$dispatch['params']);
break; break;
case 'closure': case 'closure':
// 规则闭包 // 规则闭包

View File

@@ -318,17 +318,17 @@ class Route
if (isset($array[1])) { if (isset($array[1])) {
self::parseUrlParams($array[1]); self::parseUrlParams($array[1]);
} }
$return = ['type' => 'class', 'class' => self::$bind['class'], 'method' => $array[0] ?: '', 'params' => []]; $return = ['type' => 'callable', 'callable' => [self::$bind['class'], $array[0] ?: Config::get('default_action')], 'params' => []];
break; break;
case 'namespace': case 'namespace':
// 绑定到命名空间 // 绑定到命名空间
$array = explode('/', $url, 3); $array = explode('/', $url, 3);
$class = isset($array[0]) ? $array[0] : 'index'; $class = isset($array[0]) ? $array[0] : Config::get('default_controller');
$method = isset($array[1]) ? $array[1] : 'index'; $method = isset($array[1]) ? $array[1] : Config::get('default_action');
if (isset($array[2])) { if (isset($array[2])) {
self::parseUrlParams($array[2]); self::parseUrlParams($array[2]);
} }
$return = ['type' => 'class', 'class' => self::$bind['namespace'] . '\\' . $class, 'method' => $method, 'params' => []]; $return = ['type' => 'callable', 'callable' => [self::$bind['namespace'] . '\\' . $class, $method], 'params' => []];
break; break;
case 'module': case 'module':
// 如果有模块/控制器绑定 针对路由到 模块/控制器 有效 // 如果有模块/控制器绑定 针对路由到 模块/控制器 有效
@@ -580,14 +580,12 @@ class Route
$item = substr($item, 1, -1); $item = substr($item, 1, -1);
} }
if (0 === strpos($item, ':')) { if (0 === strpos($item, ':')) {
if (strpos($item, '\\')) { if (strpos($item, '\\')) {
$var = substr($item, 1, -2); $var = substr($item, 1, -2);
} else { } else {
$var = substr($item, 1); $var = substr($item, 1);
} }
$matches[$var] = array_shift($paths); $matches[$var] = array_shift($paths);
} else { } else {
// 过滤URL中的静态变量 // 过滤URL中的静态变量
array_shift($paths); array_shift($paths);
@@ -602,8 +600,8 @@ class Route
} }
$result = ['type' => 'redirect', 'url' => $url, 'status' => (is_array($route) && isset($route[1])) ? $route[1] : 301]; $result = ['type' => 'redirect', 'url' => $url, 'status' => (is_array($route) && isset($route[1])) ? $route[1] : 301];
} elseif (0 === strpos($url, '\\')) { } elseif (0 === strpos($url, '\\')) {
// 路由到行为 // 路由到回调
$result = ['type' => 'class', 'class' => $url, 'method' => isset($route[1]) ? $route[1] : '', 'params' => $matches]; $result = ['type' => 'callable', 'callable' => $route, 'params' => $matches];
} elseif (0 === strpos($url, '@')) { } elseif (0 === strpos($url, '@')) {
// 路由到控制器 // 路由到控制器
$result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches]; $result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches];