完善响应类

This commit is contained in:
TOP糯米 2019-08-31 16:03:41 +08:00
parent f1f48f4a2c
commit 6897c11b02
3 changed files with 26 additions and 10 deletions

View File

@ -44,10 +44,12 @@ class App
$response = Response::instance(); $response = Response::instance();
// 处理请求并得到数据 // 处理请求并得到数据
$responseData = $response->dispatch($request->execute($type, $defaultModule)); $responseData = $response->header([
'X-Powered-By: TOP-Framework'
])->dispatch($request->execute($type, $defaultModule));
// 输出内容 // 输出内容
echo $responseData; echo $responseData->content;
} }
} }

View File

@ -19,7 +19,7 @@ class Response
* 响应内容 * 响应内容
* @var null * @var null
*/ */
private $content = null; public $content = null;
/** /**
* 响应头 * 响应头
@ -29,11 +29,19 @@ class Response
/** /**
* 设置Header * 设置Header
* @param array $header * @param null $header
* @return $this * @return $this
*/ */
public function header($header = []) public function header($header = null)
{ {
if (is_array($header)) {
$this->header = array_merge($this->header, $header);
} else {
$this->header[] = $header;
}
foreach ($this->header as $value) {
header($value);
}
return $this; return $this;
} }
@ -44,11 +52,15 @@ class Response
*/ */
public function dispatch($data) public function dispatch($data)
{ {
// 处理响应数据,并返回 if ($data instanceof Response) {
$responseData = new ResponseData($data); return $data;
$this->content = $responseData->dispatch(); } else {
// 处理响应数据,并返回
$responseData = new ResponseData($data);
$this->content = $responseData->dispatch();
return $this->content; return $this;
}
} }
} }

View File

@ -19,7 +19,9 @@ class View implements MiddlewareIfs
$cache = File::instance($config['cacheDir']); $cache = File::instance($config['cacheDir']);
if ($cache->exists($ident)) { if ($cache->exists($ident)) {
$content = $cache->get($ident); $content = $cache->get($ident);
return Response::instance()->dispatch($content); return Response::instance()->header([
'HTTP/1.1 304 Not Modified'
])->dispatch($content);
} }
} }
return true; return true;