diff --git a/framework/library/Application.php b/framework/library/Application.php index 71131e7..74da3a6 100644 --- a/framework/library/Application.php +++ b/framework/library/Application.php @@ -83,10 +83,10 @@ class Application // 处理请求并得到数据 $response = Response::instance()->header([ - 'X-Powered-By: TOP-Framework' + 'X-Powered-By: TOP-Framework', ])->send($router->execute()); - - // 响应内容 + + // 统一输出响应内容 echo $response->content; } diff --git a/framework/library/exception/ResponseException.php b/framework/library/exception/ResponseException.php new file mode 100644 index 0000000..907504c --- /dev/null +++ b/framework/library/exception/ResponseException.php @@ -0,0 +1,19 @@ +header as $value) { header($value); } + return $this; } @@ -118,7 +119,7 @@ class Response 'Status ' . $text, ]); } - throw new Exception('不支持的状态码:' . $code); + throw new ResponseException('不支持的状态码:' . $code); } /** @@ -148,6 +149,23 @@ class Response } } + /** + * 输出文件 + * @param $filename + * @param $name + * @return Response + */ + public function sendFile($filename = null, $name = null) + { + if (is_file($filename)) { + $name = ($name) ? $name : uniqid() . '.' . substr($filename, strrpos($filename, '.') + 1); + return $this->header([ + 'Content-Disposition: attachment; filename="' . $name . '"', + ])->code(200)->send(readfile($filename)); + } + throw new ResponseException('不存在的文件:' . $filename); + } + /** * 处理数据 * @param $data @@ -173,13 +191,4 @@ class Response return $data; } - /** - * 直接echo处理 - * @return string - */ - public function __toString() - { - return $this->content; - } - }