完善错误页面,可在配置文件根据HTTP状态码指定页面展示

This commit is contained in:
TOP糯米 2020-05-30 01:48:06 +08:00
parent 73fd8c9941
commit 2c3e516310
5 changed files with 104 additions and 20 deletions

View File

@ -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);
}
}

View File

@ -1,11 +1,13 @@
<?php
// 默认配置
return [
'default_controller' => 'Index',
'default_method' => 'index',
'compel_route' => false,
'complete_parameter' => true,
'error_pages' => [
404 => '.' . DS . '404.html',
],
'register' => [
'Top' => \top\library\template\driver\Top::class,
],

View File

@ -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;
}

View File

@ -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 <<<EOF
页面找不到了
EOF;
}
/**
* 处理数据
* @param $data

11
public/404.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 Not Found</title>
</head>
<body>
<script type="text/javascript" src="https://qzonestyle.gtimg.cn/qzone_v6/lostchild/search_children.js"></script>
</body>
</html>