Skip to content

Commit

Permalink
Merge pull request #6 from nadirvishun/1.2
Browse files Browse the repository at this point in the history
增加生产环境下错误页展示
  • Loading branch information
nadirvishun authored Sep 23, 2017
2 parents 14a6408 + 4b63ef5 commit b26bb34
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@
上方仅仅是主要的,还有其它各种各样的,更别说组件本身各种细节的实现和整合,所以说要想写一个框架真是超级麻烦。

## TODO
- 路由参照lumen进一步封装,引入统一命名空间配置等
- cli引入,没有这个只能在网页浏览,太难受
- 路由参照lumen进一步封装,引入统一命名空间配置等
158 changes: 158 additions & 0 deletions app/views/errors/error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>HTTP 404 - SegmentFault</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
line-height: 1.7;
margin: 0;
box-sizing: border-box;
}
html {
background-color: #f3f3f3;
color: #888;
display: table;
font-family: Helvetica, "Helvetica Neue", Arial, sans-serif;
height: 100%;
width: 100%;
}
body {
display: table-cell;
vertical-align: middle;
margin: 2em auto;
}
h1 {
color: #555;
font-size: 2em;
}
a {
color: #06a;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
p, .p {
margin: 10px 0 15px;
}
input, button {
font-size: 16px;
padding: 6px 10px;
border: none;
border-radius: 2px;
margin-top:40px;
}
input {
background-color: #f3f3f3;
width: 60%;
}
button {
min-width: 80px;
background-color: #77B767;
text-align: center;
color: #fff;
cursor: pointer;
}
button:hover,
button:active {
background-color: #008151;
}
.error {
color: #d9534f;
}
.text-center {
text-align: center;
}
.box {
margin: 0 auto;
padding: 40px;
background-color: #fff;
width: 540px;
}
.figure {
float: right;
line-height: 0;
}
.figure img {
height: 200px;
}
.footer {
margin: 15px 0 0;
color: #999;
text-transform: uppercase;
font-size: 13px;
}
.footer a {
color: #999;
}
@media only screen and (max-width: 480px) {
.box {
padding: 20px;
width: 100%;
}
h1 {
font-size: 1.5em;
margin: 0 0 0.3em 0;
}
.figure {
float: none;
}
}
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
</style>
</head>
<body>
<div class="box">
<div class="clearfix">
<figure class="figure">
<img src="img/error.jpg" alt="杯具啊">
</figure>
<h1>呃呃呃!</h1>
<div class="p">我去!这网页有毒.....</div>
<div class="p">
<button type="button" onclick="window.location.href='/'">回首页</button>
</div>
</div>
</div>
<div class="footer text-center">
1024 &copy; Modernphp
</div>
</body>
</html>
15 changes: 12 additions & 3 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct($config=[])
{
\Modern::$app=$this;
$this->init($config);
// $this->registerErrorHandler();
$this->registerErrorHandler();
parent::__construct($config);
}
/**
Expand Down Expand Up @@ -123,8 +123,17 @@ public function registerErrorHandler()
//增加名称为app.exception,以区分开来
$this->logger->withName('app.exception')->error($exception->getMessage());//纪录日志(发现laravel和yii2等都将此纪录的等级设为error,没有再细分)
$this->clearOutput();
//TODO,指向错误视图页
echo '-_-!!,悲剧了!';//展示自定义错误信息而不暴露详细信息
//展示自定义错误信息而不暴露详细信息
$errorFile=APP_PATH.DS.'views'.DS.'errors'.DS.'error.php';
if(file_exists($errorFile)){
ob_start();
ob_implicit_flush(false);
require($errorFile);
echo ob_get_clean();
}else{
//T指向错误视图页
echo '-_-!!,悲剧了!';
}
});
}
$whoops->register();
Expand Down
3 changes: 2 additions & 1 deletion core/helpers/Inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public static function camel2id($name, $separator = '-', $strict = false)
return trim(strtolower(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name))), $separator);
}
}

/**
* 驼峰转ID
* ID转驼峰
* 类似hello-world转为HelloWorld
*/
public static function id2camel($id, $separator = '-')
Expand Down
Binary file added public/img/error.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* 还可以继续细分,同时引入命名空间的分组等,可参考lumen框架中的处理,这里不做进一步处理了
*/
return function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/users', function () {//测试匿名函数
echo '123';
$r->addRoute('GET', '/', function () {//测试匿名函数
echo '这是首页';
});
//不支持设置统一的命名空间,暂时只能写全路径
$r->addRoute('GET', '/dirty/test', '\app\controllers\Dirty@test');//测试流相关
Expand Down
25 changes: 25 additions & 0 deletions storage/logs/app-2017-09-01.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[2017-09-01 12:00:50] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:00:50] app.exception.ERROR: modernphp\Application::modernphp\{closure}(): Failed opening required '' (include_path='.;C:\php\pear') [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:00:50] app.exception.ERROR: Undefined variable: viewFile [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:01:03] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:01:44] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:03:58] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:04:53] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:04:57] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:05:29] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:06:52] app.exception.ERROR: 404 Not Found [] {"url":"/sl","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:07:01] app.exception.ERROR: 404 Not Found [] {"url":"/","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":"http://www.mordenphp.com/sl"}
[2017-09-01 12:07:02] app.exception.ERROR: 404 Not Found [] {"url":"/","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":"http://www.mordenphp.com/"}
[2017-09-01 12:07:07] app.exception.ERROR: 404 Not Found [] {"url":"/","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 12:08:56] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 17:26:28] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 17:26:28] app.exception.ERROR: 404 Not Found [] {"url":"/public/img/error.jpg","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":"http://www.mordenphp.com/sfs"}
[2017-09-01 17:26:57] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 17:26:57] app.exception.ERROR: 404 Not Found [] {"url":"/public/img/error.jpg","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":"http://www.mordenphp.com/sfs"}
[2017-09-01 17:27:58] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 17:27:58] app.exception.ERROR: 404 Not Found [] {"url":"/public/img/error.jpg","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":"http://www.mordenphp.com/sfs"}
[2017-09-01 17:28:13] app.exception.ERROR: 404 Not Found [] {"url":"/public/img/error.jpg","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":"http://www.mordenphp.com/sfs"}
[2017-09-01 17:28:35] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 17:30:05] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 17:31:41] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}
[2017-09-01 17:32:10] app.exception.ERROR: 404 Not Found [] {"url":"/sfs","ip":"127.0.0.1","http_method":"GET","server":"www.mordenphp.com","referrer":null}

0 comments on commit b26bb34

Please sign in to comment.