public function dispatch() { $view_controllerName = $this->controllerName; $view_actionName = $this->actionName; $controllerClassName = ucfirst($this->controllerName) . 'Controller'; if (file_exists('./controllers/' . $controllerClassName . '.php')) { require './controllers/' . $controllerClassName . '.php'; } else { $view_controllerName = DEFAULT_CONTROLLER_NAME; $controllerClassName = DEFAULT_CONTROLLER_NAME . 'Controller'; Basic::requireOr404('./controllers/' . $controllerClassName . '.php'); } //把controller中输出的内容保留并在view后输出 ob_start(); Lugit::$controller = new $controllerClassName($this->array_parameter); if (method_exists(Lugit::$controller, $this->actionName . 'Action')) { //方法存在,调用方法 Lugit::$controller->{$this->actionName . 'Action'}(); } else { if (method_exists(Lugit::$controller, DEFAULT_ACTION_NAME . 'Action')) { //方法不存在,调用默认方法 $view_actionName = DEFAULT_ACTION_NAME; Lugit::$controller->{DEFAULT_ACTION_NAME . 'Action'}($this->actionName); } else { //方法不存在 且 默认方法不存在,返回404错误 throw new NotFoundException(); } } $controller_output = ob_get_clean(); if (empty(Lugit::$view)) { Lugit::$view = new View(Lugit::$controller->getVars()); } $template = isset($this->template) ? $this->template : './views/scripts/' . $view_controllerName . '/' . $view_actionName . '.phtml'; Lugit::$view->render($template); echo $controller_output; }