错误处理程序Laravel中的访问请求对象

时间:2016-01-25 15:28:53

标签: php laravel laravel-5.2

在我的Laravel 5.2项目中,我有一个中间件,可以愉快地存储对数据库或文件的请求和响应。 在那里我序列化了/ json_encode $request对象来记录一切正在进行的事情。 (cookies,输入,文件,标题......)

我需要创建一个错误处理程序,它将使用整个请求对象将有关请求的所有内容包含在报告电子邮件中。但ExceptionHandler::report()不接受请求作为参数。

2 个答案:

答案 0 :(得分:3)

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);
}

答案 1 :(得分:2)

在App \ Exceptions \ Handler.php中,渲染方法确实将请求作为参数。 在这里,您可以触发事件以将内容存储在会话或数据库中。

例如:

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.