【发布时间】:2020-09-22 06:33:23
【问题描述】:
在我的 Laravel 5.1 中。应用程序我使用隐式模型绑定。但是我想问一下,如果数据库中不存在被访问的资源,我该如何提供自定义响应?因此,当找不到资源时,我得到:
404 Not Found
"class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
我尝试使用以下代码更改/app/Exceptions下的Handler.php中的渲染方法
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
dd('model not found');
//$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
什么都没有改变,dd 方法没有任何影响。我将代码更改为:
public function render($request, Exception $e)
{
if ($e instanceof NotFoundHttpException) {
dd('model not found');
//$e = new NotFoundHttpException($e->getMessage(), $e);
}
return parent::render($request, $e);
}
也没有任何改变。当我尝试访问的资源不存在时,我仍然收到相同的响应。
【问题讨论】:
标签: laravel laravel-5 http-response-codes