我在Lumen 5.5上运行了一个小应用程序,在我的错误处理程序中,当我传递一个视图作为响应的内容时,报头得到一个错误500而不是404。
我已经添加了一个样本片段,请考虑我只会返回一个或另一个响应!
文件: app/Exceptions/Handler.php
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e) {
if($e instanceof NotFoundHttpException) {
// This gives me a 404 in the browser dev console but not in the headers
return response(view("errors.404"), 404);
// This gives me a 404 in the headers
return response('404 error', 404);
}
return parent::render($request, $e);
}如果我在浏览器DevTools中加载这个页面,GET的状态为404,但如果我使用http检查工具在线扫描,我会得到一个错误500。
这扰乱了我的广告宣传活动,所以我不得不换成一个简单的回应。
因为它是Lumen,所以我不能使用以下在Laravel中可以工作的代码:
return response(view('error.404', [], 404));非常提前感谢您。
发布于 2017-11-02 15:24:46
这样如何:
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
if ($e instanceof NotFoundHttpException) {
return new Response(
view('errors.404'),
$e->getStatusCode(),
$e->getHeaders()
);
}https://stackoverflow.com/questions/47061684
复制相似问题