Handler.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Auth\AuthenticationException;
  5. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  6. class Handler extends ExceptionHandler
  7. {
  8. /**
  9. * A list of the exception types that should not be reported.
  10. *
  11. * @var array
  12. */
  13. protected $dontReport = [
  14. \Illuminate\Auth\AuthenticationException::class,
  15. \Illuminate\Auth\Access\AuthorizationException::class,
  16. \Symfony\Component\HttpKernel\Exception\HttpException::class,
  17. \Illuminate\Database\Eloquent\ModelNotFoundException::class,
  18. \Illuminate\Session\TokenMismatchException::class,
  19. \Illuminate\Validation\ValidationException::class,
  20. ];
  21. /**
  22. * Report or log an exception.
  23. *
  24. * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
  25. *
  26. * @param \Exception $exception
  27. * @return void
  28. */
  29. public function report(Exception $exception)
  30. {
  31. parent::report($exception);
  32. }
  33. /**
  34. * Render an exception into an HTTP response.
  35. *
  36. * @param \Illuminate\Http\Request $request
  37. * @param \Exception $exception
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function render($request, Exception $e)
  41. {
  42. /**
  43. * update by pjh 20180306
  44. * 根据异常错误码响应到views/errors中对应视图
  45. **/
  46. if ($e instanceof HttpException) {
  47. $code = $e->getStatusCode();
  48. if (view()->exists('errors.'.$code)) {
  49. return response()->view('errors.'.$e->getStatusCode());
  50. }
  51. }
  52. return parent::render($request, $e);
  53. }
  54. /**
  55. * Convert an authentication exception into an unauthenticated response.
  56. *
  57. * @param \Illuminate\Http\Request $request
  58. * @param \Illuminate\Auth\AuthenticationException $exception
  59. * @return \Illuminate\Http\Response
  60. */
  61. protected function unauthenticated($request, AuthenticationException $exception)
  62. {
  63. if ($request->expectsJson()) {
  64. return response()->json(['error' => 'Unauthenticated.'], 401);
  65. }
  66. if(in_array('admin', $exception->guards())) {
  67. return redirect()->guest('admin/login');
  68. }
  69. return redirect()->guest(route('login'));
  70. }
  71. }