App类增加dispatch方法可以进行手动注册调度机制 无需经过路由

This commit is contained in:
thinkphp
2016-01-04 19:01:12 +08:00
parent 84e78a5a4a
commit d2b637d803

View File

@@ -18,6 +18,11 @@ namespace think;
class App class App
{ {
// 应用调度机制
private static $dispatch = [];
// 应用调度类型
private static $type = '';
/** /**
* 执行应用程序 * 执行应用程序
* @access public * @access public
@@ -72,37 +77,39 @@ class App
Session::init($config['session']); Session::init($config['session']);
} }
// URL路由检测 if (empty(self::$type)) {
$dispatch = self::route($config); // 未指定调度类型 则进行URL路由检测
self::route($config);
}
// 监听app_begin // 监听app_begin
APP_HOOK && Hook::listen('app_begin', $dispatch); APP_HOOK && Hook::listen('app_begin', $dispatch);
// 根据类型调度 // 根据类型调度
switch ($dispatch['type']) { switch (self::$type) {
case 'redirect': case 'redirect':
// 执行重定向跳转 // 执行重定向跳转
header('Location: ' . $dispatch['url'], true, $dispatch['status']); header('Location: ' . self::$dispatch['url'], true, self::$dispatch['status']);
break; break;
case 'module': case 'module':
// 模块/控制器/操作 // 模块/控制器/操作
$data = self::module($dispatch['data'], $config); $data = self::module(self::$dispatch['data'], $config);
break; break;
case 'action': case 'action':
// 执行操作 // 执行操作
$data = Loader::action($dispatch['action'], $dispatch['params']); $data = Loader::action(self::$dispatch['action'], self::$dispatch['params']);
break; break;
case 'behavior': case 'behavior':
// 执行行为 // 执行行为
$data = Hook::exec($dispatch['class'], $dispatch['method'], $dispatch['params']); $data = Hook::exec(self::$dispatch['class'], self::$dispatch['method'], self::$dispatch['params']);
break; break;
case 'regex_closure': case 'regex_closure':
// 正则闭包 // 正则闭包
$data = self::invokeRegex($dispatch['closure'], $dispatch['params']); $data = self::invokeRegex(self::$dispatch['closure'], self::$dispatch['params']);
break; break;
case 'rule_closure': case 'rule_closure':
// 规则闭包 // 规则闭包
$data = self::invokeRule($dispatch['closure'], $dispatch['params']); $data = self::invokeRule(self::$dispatch['closure'], self::$dispatch['params']);
break; break;
default: default:
throw new Exception('dispatch type not support', 10008); throw new Exception('dispatch type not support', 10008);
@@ -436,7 +443,14 @@ class App
$result = Route::parseUrl($path_info, $depr); $result = Route::parseUrl($path_info, $depr);
} }
} }
return $result; // 注册调度机制
self::dispatch($result);
} }
// 指定应用调度类型和参数
public static function dispatch($dispatch)
{
self::$dispatch = $dispatch;
self::$type = $dispatch['type'];
}
} }