/** * 分发到控制器 * 1. 检验action是否允许执行,如果不允许执行则抛出异常,中断执行,否则执行下一步,注:[controller 的私有方法&&保留方法&&静态方法不可执行(view调用controller方法专用)], * 2. 进行初始化 * 3. 执行action * 4. 执行send输出 * @param string * @throws \Exception */ public function run($method) { // 检测action是否可以执行 $bool = method_exists($this, $method) && ($reflector = new \ReflectionMethod($this, $method)); if (!$bool || !$reflector->isPublic() || $reflector->isStatic() || $method == 'run' || $method == 'initialize' || $method == 'send') { \Core\Application::abort(404, 'action not exits!'); } // 如果不想让他顺序进行抛出异常即可中断 $this->initialize(); $this->{$method}(); $this->send(); }
/** * 执行 */ public static function dispatch($pattern_map, $url) { $class_action = \Core\Router::match_all($pattern_map, $url); $parts = explode('::', $class_action); $class_file = strtolower(W_APPLICATION_PATH . $parts[0] . W_EXT); if (file_exists($class_file)) { $parts[0] = str_replace('/', '\\', $parts[0]); $class = new $parts[0](); $path = explode('/', $parts[1]); $action = array_shift($path); $class->run($action); } else { \Core\Application::abort(404, 'dispatch 404'); } }