diff --git a/framework/Framework.php b/framework/Framework.php index d89211b..1677edd 100644 --- a/framework/Framework.php +++ b/framework/Framework.php @@ -117,9 +117,9 @@ class Framework $scriptName = $_SERVER['SCRIPT_NAME']; $pos = strrpos($scriptName, '/'); $root = substr($scriptName, 0, $pos + 1); - $path = $root . 'resource' . DS; + $path = $root . 'resource' . '/'; } - define('RESOURCE', str_replace('/', DS, $path)); + define('RESOURCE', $path); } } diff --git a/framework/config/config.php b/framework/config/config.php index 392228a..e366d01 100644 --- a/framework/config/config.php +++ b/framework/config/config.php @@ -1,11 +1,13 @@ 'Index', 'default_method' => 'index', 'compel_route' => false, 'complete_parameter' => true, + 'error_pages' => [ + 404 => '.' . DS . '404.html', + ], 'register' => [ 'Top' => \top\library\template\driver\Top::class, ], diff --git a/framework/library/Router.php b/framework/library/Router.php index 7e18faf..b48fd88 100644 --- a/framework/library/Router.php +++ b/framework/library/Router.php @@ -244,7 +244,7 @@ class Router $this->handler($this->request->uri()); } catch (RouteException $exception) { if (!DEBUG) { // 非调试模式直接404 - return \response()->notFound(); + return \response()->code(404)->send(); } else throw $exception; } diff --git a/framework/library/http/Response.php b/framework/library/http/Response.php index 3fcda12..a2b2268 100644 --- a/framework/library/http/Response.php +++ b/framework/library/http/Response.php @@ -2,6 +2,7 @@ namespace top\library\http; +use Exception; use top\library\View; use top\traits\Instance; use top\traits\Json; @@ -30,6 +31,60 @@ class Response */ private $header = []; + /** + * HTTP状态码 + * @var integer + */ + private $code = 200; + + /** + * HTTP状态码详情 + * @var array + */ + private $codeDetail = [ + 100 => 'Continue', + 101 => 'Switching Protocols', + 200 => 'OK', + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Moved Temporarily ', + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 307 => 'Temporary Redirect', + 400 => 'Bad Request', + 401 => 'Unauthorized', + 402 => 'Payment Required', + 403 => 'Forbidden', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 406 => 'Not Acceptable', + 407 => 'Proxy Authentication Required', + 408 => 'Request Timeout', + 409 => 'Conflict', + 410 => 'Gone', + 411 => 'Length Required', + 412 => 'Precondition Failed', + 413 => 'Request Entity Too Large', + 414 => 'Request-URI Too Long', + 415 => 'Unsupported Media Type', + 416 => 'Requested Range Not Satisfiable', + 417 => 'Expectation Failed', + 500 => 'Internal Server Error', + 501 => 'Not Implemented', + 502 => 'Bad Gateway', + 503 => 'Service Unavailable', + 504 => 'Gateway Timeout', + 505 => 'HTTP Version Not Supported', + 509 => 'Bandwidth Limit Exceeded', + ]; + /** * 设置Header * @param null $header @@ -48,35 +103,51 @@ class Response return $this; } + /** + * 设置响应状态码 + * @param $code + * @return Response + */ + public function code($code = 200) + { + if (isset($this->codeDetail[$code])) { + $this->code = $code; + $text = $code . ' ' . $this->codeDetail[$code]; + return $this->header([ + 'HTTP/1.1 ' . $text, + 'Status ' . $text, + ]); + } + throw new Exception('不支持的状态码:' . $code); + } + /** * 返回内容 * @param $data - * @return false|int|null|string + * @return Response */ - public function send($data) + public function send($data = null) { if ($data instanceof Response) { return $data; } else { - // 处理响应数据,并返回 - $this->content = $this->getContent($data); + $pages = config('error_pages'); + if (isset($pages[$this->code])) { + $filename = $pages[$this->code]; + if (is_file($filename)) { + $this->content = file_get_contents($filename); + } else { + $this->content = ''; + } + } else { + // 处理响应数据,并返回 + $this->content = $this->getContent($data); + } return $this; } } - public function notFound() - { - $this->header([ - 'HTTP/1.1 404 Not Found', - 'Status: 404 Not Found' - ]); - return << + + + + + 404 Not Found + + + + +