hctopcu hctopcu -4 years ago 147
PHP Question

Access Request Object in Error Handler Laravel

In my Laravel 5.2 project, I have a middleware happily storing requests and responses to DB or files.
There I serialize/json_encode

$request
object for logging everything going on. (cookies, input, files, headers...)

I need to create an error handler that will use whole request object to include everything about request into the report email. But
ExceptionHandler::report()
does not accept Request as a parameter.

Answer Source

In App\Exceptions\Handler.php and the render method wich does have the request as parameter. here you could fire an event to store the stuff in a session or database.

For example:

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

more info here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download