function onRequest(Swoole\Request $request) { $response = new Swoole\Response(); $php = Swoole::getInstance(); $request->setGlobal(); // if($this->doStaticRequest($request, $response)) // { // return $response; // } //将对象赋值到控制器 $php->request = $request; $php->response = $response; $response->head['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'; $response->head['Pragma'] = 'no-cache'; try { ob_start(); /*---------------------处理MVC----------------------*/ $response->body = $php->runMVC(); $response->body .= ob_get_contents(); ob_end_clean(); } catch (\Exception $e) { if ($request->finish != 1) { $this->http_error(404, $response, $e->getMessage()); } } if (!isset($response->head['Content-Type'])) { $response->head['Content-Type'] = 'text/html; charset=' . $this->config['apps']['charset']; } //重定向 if (isset($response->head['Location'])) { $response->send_http_status(301); } return $response; }
/** * 将swoole扩展产生的请求对象数据赋值给框架的Request对象 * @param Swoole\Request $request */ function assign(Swoole\Request $request) { if (isset($this->request->get)) { $request->get = $this->request->get; } if (isset($this->request->post)) { $request->post = $this->request->post; } if (isset($this->request->files)) { $request->files = $this->request->files; } if (isset($this->request->cookie)) { $request->cookie = $this->request->cookie; } if (isset($this->request->server)) { foreach ($this->request->server as $key => $value) { $request->server[strtoupper($key)] = $value; } $request->remote_ip = $this->request->server['remote_addr']; } $request->header = $this->request->header; $request->setGlobal(); }
/** * 动态请求 * @param $request * @param $response * @return unknown_type */ function processDynamic(Swoole\Request $request, Swoole\Response $response) { $path = $this->document_root . '/' . $request->meta['path']; if (is_file($path)) { $request->setGlobal(); $response->head['Content-Type'] = 'text/html'; ob_start(); try { include $path; $response->body = ob_get_contents(); } catch (\Exception $e) { $response->setHttpStatus(500); $response->body = $e->getMessage() . '!<br /><h1>' . self::SOFTWARE . '</h1>'; } ob_end_clean(); } else { $this->httpError(404, $response, "页面不存在({$request->meta['path']})!"); } }
/** * 动态请求 * @param $request * @param $response * @return unknown_type */ function processDynamic(Swoole\Request $request, Swoole\Response $response) { $request->setGlobal(); $response->head['Content-Type'] = 'text/html'; ob_start(); try { $this->application->bootstrap(); $request_uri = $request->meta['path']; $this->application->getDispatcher()->dispatch(new \Yaf_Request_Http($request_uri)); $response->body = ob_get_contents(); } catch (\Exception $e) { $response->setHttpStatus(500); $response->body = $e->getMessage() . '!<br /><h1>' . self::SOFTWARE . '</h1>'; } ob_end_clean(); }