From d5a407b984379837e45f86a3735a24a362d0ff6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=A8=E8=9F=B9=E5=B0=8F=E6=98=9F=E4=BA=91?= <5353993+jxxxysoft@user.noreply.gitee.com> Date: Sun, 2 Jan 2022 09:52:39 +0000 Subject: [PATCH] =?UTF-8?q?=E8=AE=A9404=E9=A1=B5=E9=9D=A2=E6=9B=B4?= =?UTF-8?q?=E5=8A=A0=E7=94=9F=E5=8A=A8=E5=BD=A2=E8=B1=A1[=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E6=BB=91=E7=A8=BD]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- startmvc/Boot.php | 11 ++++--- startmvc/Function.php | 68 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/startmvc/Boot.php b/startmvc/Boot.php index ea1c729..9c76103 100644 --- a/startmvc/Boot.php +++ b/startmvc/Boot.php @@ -94,12 +94,15 @@ class Boot } private function startApp($module, $controller, $action, $argv) { $controller = APP_NAMESPACE.'\\' . ($module != '' ? $module . '\\' : '') . 'Controller\\' . $controller . 'Controller'; - if (!class_exists($controller)) { - header("HTTP/1.1 404 Not Found"); - header("Status: 404 Not Found"); + $action .= 'Action'; + if (!class_exists($controller) || !method_exists($controller, $action)) { + if ($this->conf['debug']) { + echo '控制器 ' . $controller . ' 或 方法 ' . $action . '不存在'; + } else { + showErroPage(404); + } die(); } - $action .= 'Action'; Lib\Loader::make($controller, $action, $argv); } diff --git a/startmvc/Function.php b/startmvc/Function.php index 959a450..48ff630 100644 --- a/startmvc/Function.php +++ b/startmvc/Function.php @@ -32,3 +32,71 @@ } return $key?$lang[$key]:$key; } + /** + * erro页面的处理 + * @param int 错误代码 + */ + function showErroPage($errorCode=404) + { + sendHttpStatus($errorCode); + die('404 Not Found

4 0 4

大事不妙啦!

你访问的页面好像不存在

'); + } + /** + * 发送状态码 + * @param int 状态码 + */ + function sendHttpStatus($i_status) + { + $a_status = array( + // Informational 1xx + 100 => 'Continue', + 101 => 'Switching Protocols', + // Success 2xx + 200 => 'OK', + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + // Redirection 3xx + 300 => 'Multiple Choices', + 301 => 'Moved Permanently', + 302 => 'Found', // 1.1 + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', // 306 is deprecated but reserved + 307 => 'Temporary Redirect', + // Client Error 4xx + 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', + // Server Error 5xx + 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' + ); + + if (array_key_exists($i_status, $a_status)) { + header('HTTP/1.1 ' . $i_status . ' ' . $a_status[$i_status]); + } + } -- Gitee