laravel安装Whoops样式错误处理器教程

先上图:

 

安装步骤:

1、打开你的laravel项目目录

2、Shift+右键在这里打开命令行

3、输入命令:

composer require filp/whoops:~1.0.

4、修改 app/Exceptions/Handler.php 

render()方法中添加一个Whoops样式的处理情况,就像下面这样:

 1     /**
 2      * Render an exception into an HTTP response.
 3      *
 4      * @param  \Illuminate\Http\Request  $request
 5      * @param  \Exception                $exception
 6      * @return \Illuminate\Http\Response
 7      */
 8     public function render($request, Exception $exception)
 9     {
10         if ($this->isHttpException($exception)) {
11             return $this->renderHttpException($exception);
12         }
13 
14         if (config('app.debug')) {
15             return $this->renderExceptionWithWhoops($exception);
16         }
17 
18         return parent::render($request, $exception);
19     }
20 
21     /**
22      * Render an exception using Whoops.
23      *
24      * @param  \Exception                $exception
25      * @return \Illuminate\Http\Response
26      */
27     protected function renderExceptionWithWhoops(Exception $exception)
28     {
29         $json = new \Whoops\Handler\JsonResponseHandler;
30         $json->onlyForAjaxRequests(true);
31 
32         $whoops = new \Whoops\Run;
33         $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
34         $whoops->pushHandler($json);
35 
36         return new \Illuminate\Http\Response(
37             $whoops->handleException($exception),
38             $exception->getStatusCode(),
39             $exception->getHeaders()
40         );
41     }

然后自己写一个错误测试一下!

如图:

 

posted @ 2017-08-14 10:08  丶老中医  阅读(870)  评论(0编辑  收藏  举报
一切已经开始©2018 丶老中医