Laravel5.5的异常捕获和处理
一般在web开发中,我们很少会接触到除了404页面其它的http通信错误页面,即使是做404页面,我们也只是单纯的修改框架自带了404模板,很少能够做到完全的控制。但Laravel5改变了这种现状,那么,我们去看看如何使用Laravel5自定义错误页面的。
Laravel5是如何处理异常的
Laravel5将所有的自定义错误和异常处理都移到了App/Exceptions/Hander.php。早期Laravel的报错页面会显示'Whoops...',然后显示出错误信息,laravel后期还专门弄了个花哨的错误异常页面,可能是觉得异常页面多数在项目上线之后会被关闭或者自定义错误页面,所以就回到了最初的美好---'Whoops...'.
在hander里面调用的方法如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/*** Render an exception into an HTTP response.** @param \Illuminate\Http\Request $request * @param \Exception $e* @return \Illuminate\Http\Response*/public function render($request, Exception $e){if ($this->isHttpException($e)){return $this->renderHttpException($e);}else{return (new SymfonyDisplayer(config('app.debug')))->createResponse($e);}} |
所有的HTTP异常(如404和503),它们都是使用renderHttpException方法,它在\Illuminate\Foundation\Exceptions\Handler下可以找见。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/*** Render the given HttpException.** @param \Symfony\Component\HttpKernel\Exception\HttpException $e * @return \Symfony\Component\HttpFoundation\Response */ protected function renderHttpException(HttpException $e){if (view()->exists('errors.'.$e->getStatusCode())) { return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode()); } else { return (new SymfonyDisplayer(config('app.debug')))->createResponse($e);}} |
我们看到view方法下定义了视图存在的位置,{'errors'.$e->getStatusCode()},OK,现在就明白了,Http错误页面就是对应在views/errors/[code].blade.php文件,比如:404.blade.php,503.blade.php等等。若是对应的code错误页面不存在,会使用默认的Whoop抛出异常。
我在laravel4中是如何处理异常的
异常捕捉是在路由文件中进行的:
|
1
2
3
4
5
6
7
8
9
10
11
|
// errorApp::error(function (Exception $exception) { Log::error($exception); $error = $exception->getMessage(); if(Request::ajax()){return Response::json(['status'=>false, 'error'=>$error]);}else{return Redirect::back()->withErrors(compact('error'));}}); |
自定义404错误页面也是在理由文件中进行的:
|
1
2
3
4
|
// 404 page not foundApp::missing(function () {return Response::view('errors.404', array(), 404);}); |
相比laravel4,laravel5将http处理独立出来无疑不体现系统架构的合理化。
在之前我们看到Laravel5的异常处理app/exceptions/Hander.php:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
class Handler extends ExceptionHandler { /*** A list of the exception types that should not be reported.** @var array*/protected $dontReport = ['Symfony\Component\HttpKernel\Exception\HttpException']; /*** Report or log an exception.** This is a great spot to send exceptions to Sentry, Bugsnag, etc.** @param \Exception $e * @return void */ public function report(Exception $e){return parent::report($e);} /*** Render an exception into an HTTP response.** @param \Illuminate\Http\Request $request * @param \Exception $e* @return \Illuminate\Http\Response*/public function render($request, Exception $e){return parent::render($request, $e);} } |
第一个方法提交日志,你可以把它提交到你们专门的日志服务器,比如我们公司有自己的鹰眼日志系统;第二个方法执行异常,你也可以在这里使用$e->getStatusCode()获取到http错误代码,根据不同的错误代码进行操作,也可以在此自定义错误模板。
其实,如果大家仔细发现。我在laravel4中对异常处理也是三个方面:日志记录、异常代码处理和自定义错误页面。Laravel5将一系列操作独立出来,那么在异常处理这一块肯定是非常好的提升了。

浙公网安备 33010602011771号