下面是我最终用来在异常通知电子邮件中获得我想要的结果的代码。我之前缺少的主要部分是我没有将真实值传递给 HtmlErrorRender 类来引发调试标志。更正后的行如下所示:
new HtmlErrorRenderer(true);
这是我现在用于 app/Exceptions/Handler.php 文件的完整代码
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Log;
use Throwable;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'password',
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $exception)
{
if ($this->shouldReport($exception)) {
$this->sendEmail($exception); // sends an email
}
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Throwable
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof \Illuminate\Session\TokenMismatchException) { //https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
return redirect()
->back()
->withInput($request->except('password'))
->with('errorMessage', 'This form has expired due to inactivity. Please try again.');
}
return parent::render($request, $exception);
}
/**
* Sends an email to the developer about the exception.
*
* @return void
*/
public function sendEmail(Throwable $exception)
{
try {
$e = FlattenException::create($exception);
$handler = new HtmlErrorRenderer(true); // boolean, true raises debug flag...
$css = $handler->getStylesheet();
$content = $handler->getBody($e);
\Mail::send('emails.exception', compact('css','content'), function ($message) {
$message
->to('youremailhere@gmail.com')
->subject('Exception: ' . \Request::fullUrl())
;
});
} catch (Throwable $ex) {
Log::error($ex);
}
}
}
$css 和 $content 被传递到 resources/views/emails/exception.blade.php 的视图中。我在该文件中的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>{!! $css ?? '' !!}</style>
</head>
<body>
{!! $content ?? '' !!}
</body>
</html>