Laravel(5.5)显然以json格式而不是whoop页面返回异常。我在哪里可以禁用这种烦人的行为并收到whoops或默认的php异常格式?
答案 0 :(得分:1)
当您发出需要JSON响应的请求时,它会为您提供JSON响应。以下标头会触发JSON响应:
X-Requested-With: XMLHttpRequest
OR
X-PJAX: true
OR
Accept: */json or *+json
如果您不想要此标准行为,则可以在app/Exceptions/Handler.php
覆盖异常处理程序。将render
函数更改为此(这是vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
的直接副本,并始终通过忽略请求标头返回whoops)
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
if (method_exists($e, 'render') && $response = $e->render($request)) {
return Router::toResponse($request, $response);
} elseif ($e instanceof Responsable) {
return $e->toResponse($request);
}
$e = $this->prepareException($e);
if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
} elseif ($e instanceof ValidationException) {
return $this->convertValidationExceptionToResponse($e, $request);
}
return $this->prepareResponse($request, $e);
}
或者,在此处添加您自己的逻辑,以确定在给出不同标题时您希望返回的内容类型。