/** * 启动程序,查找相应的文件controller,action * * @return boolean */ public function start() { $controller = $this->_route->getController(); $action = $this->_route->getAction(); $params = $this->_route->getParams(); $controller = empty($controller) ? Config::get('indexController') : $controller; $className = "App\\Controller\\" . ucfirst($controller); if (!class_exists($className)) { return Render::getInstance()->setHeaders(['HTTP/1.1 404 Not Found'])->output("404 Not Found [CONTROLLER-NO-EXISTS:{$controller}]"); } $ctrl = new $className(); $action = empty($action) ? $ctrl->getDefaultAction() : $action; $method = $action . "Action"; if (empty($action) || !method_exists($className, $method)) { return Render::getInstance()->setHeaders(['HTTP/1.1 404 Not Found'])->output("404 Not Found [ACTION-NO-EXISTS:{$controller}->{$action}]"); } $data = call_user_func_array(array($ctrl, $method), $params); if (php_sapi_name() == 'cli') { return Render::getInstance()->output($data, false); } elseif (empty($ctrl->getView())) { return Render::getInstance()->setHeaders($ctrl->getHeaders())->output($data, true); } elseif (isset($_SERVER['Content-Type']) && $_SERVER['Content-Type'] == 'application/json') { return Render::getInstance()->setHeaders($ctrl->getHeaders())->output($data, true); } return Render::getInstance()->setHeaders($ctrl->getHeaders())->view($data, $ctrl->getViewFile(), $ctrl->getLayoutFile()); }
/** * 启动程序,查找相应的文件controller,action * * @return boolean */ public function start() { $controller = $this->_route->getController(); $action = $this->_route->getAction(); $params = $this->_route->getParams(); $controller = empty($controller) ? Config::get('indexController') : $controller; $className = "App\\Controllers\\" . ucfirst($controller); if (!class_exists($className)) { throw new \Exception("CONTROLLER-NO-EXISTS:{$controller}"); return false; } $ctrl = new $className(); $action = empty($action) ? $ctrl->getDefaultAction() : $action; if (!empty($action) && !method_exists($className, $action)) { throw new \Exception("ACTION-NO-EXISTS:{$controller}->{$action}"); return false; } $data = call_user_func_array(array($ctrl, $action), $params); if (php_sapi_name() == 'cli') { Render::getInstance()->output($data, false); } elseif (empty($ctrl->getView())) { Render::getInstance()->setHeaders($ctrl->getHeaders())->output($data, true); } elseif (isset($_SERVER['Content-Type']) && $_SERVER['Content-Type'] == 'application/json') { Render::getInstance()->setHeaders($ctrl->getHeaders())->output($data, true); } Render::getInstance()->setHeaders($ctrl->getHeaders())->view($data, $ctrl->getViewFile(), $ctrl->getLayoutFile()); return true; }