错误处理程序 Laravel 中的访问请求 Object
Access Request Object in Error Handler Laravel
在我的 Laravel 5.2 项目中,我有一个中间件可以愉快地存储对数据库或文件的请求和响应。
我 serialize/json_encode $request
object 用于记录所有发生的事情。 (cookie、输入、文件、headers...)
我需要创建一个错误处理程序,它将使用整个请求 object 将有关请求的所有内容包含到报告电子邮件中。但是ExceptionHandler::report()
不接受Request作为参数。
在 App\Exceptions\Handler.php 中,render 方法将请求作为参数。
在这里您可以触发事件以将内容存储在会话或数据库中。
例如:
public function render($request, Exception $e)
{
if ($e instanceof HttpException) {
if ($e->getStatusCode() == 403) {
Event::fire(new UserNotAllowed($request));
return redirect()->to("/home");
}
if ($e->getStatusCode() == 404) {
if (Auth::guest()) {
return redirect()->to("/");
}
}
}
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
更多信息here.
Laravel 5.2 提供 the helper method request()
,适用于此用例:
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
$request = request();
parent::report($exception);
}
在我的 Laravel 5.2 项目中,我有一个中间件可以愉快地存储对数据库或文件的请求和响应。
我 serialize/json_encode $request
object 用于记录所有发生的事情。 (cookie、输入、文件、headers...)
我需要创建一个错误处理程序,它将使用整个请求 object 将有关请求的所有内容包含到报告电子邮件中。但是ExceptionHandler::report()
不接受Request作为参数。
在 App\Exceptions\Handler.php 中,render 方法将请求作为参数。 在这里您可以触发事件以将内容存储在会话或数据库中。
例如:
public function render($request, Exception $e)
{
if ($e instanceof HttpException) {
if ($e->getStatusCode() == 403) {
Event::fire(new UserNotAllowed($request));
return redirect()->to("/home");
}
if ($e->getStatusCode() == 404) {
if (Auth::guest()) {
return redirect()->to("/");
}
}
}
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
更多信息here.
Laravel 5.2 提供 the helper method request()
,适用于此用例:
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
$request = request();
parent::report($exception);
}