如何 return 为 "No query results for model"、Laravel 自定义 API 响应
How to return custom API response for "No query results for model", Laravel
我正在 Laravel 5.2.RESTful API 中构建 RESTful。
在我的资源控制器中,我想使用隐式模型绑定来显示资源。例如
public function show(User $users)
{
return $this->respond($this->userTransformer->transform($users));
}
当请求不存在的资源时Laravel自动returnS NotFoundHttpException
NotFoundHttpException
我想 return 我自己的自定义响应,但是对于使用路由模型绑定完成的查询,我该如何做到这一点?
这样的能实现吗?
或者我会坚持使用像这样的旧代码:
public function show($id)
{
$user = User::find($id);
if ( ! $user ) {
return $this->respondNotFound('User does not exist');
}
return $this->respond($this->userTransformer->transform($users));
}
所以我可以查看是否找不到资源(用户)并return适当的响应。
我认为 /app/Exceptions
下的 Handler.php
文件是个好地方
public function render($request, Exception $e)
{
if ($e instanceof NotFoundHttpException) {
// return your custom response
}
return parent::render($request, $e);
}
看看你能不能抓到 ModelNotFound
。
public function render($request, Exception $e)
{
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
dd('model not found');
}
return parent::render($request, $e);
}
在 Laravel 7 和 8 中你可以这样做。
在 app/Exception/Handler.php class 中,添加如下所示的 render() 方法(如果不存在)。
请注意,您应该使用 Throwable .
而不是类型提示 Exception class
use Throwable;
public function render($request, Throwable $e)
{
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
//For API (json)
if (request()->wantsJson()) {
return response()->json([
'message' => 'Record Not Found !!!'
], 404);
}
//Normal
return view('PATH TO YOUR ERROR PAGE');
}
return parent::render($request, $e);
}
我正在 Laravel 5.2.RESTful API 中构建 RESTful。
在我的资源控制器中,我想使用隐式模型绑定来显示资源。例如
public function show(User $users)
{
return $this->respond($this->userTransformer->transform($users));
}
当请求不存在的资源时Laravel自动returnS NotFoundHttpException
NotFoundHttpException
我想 return 我自己的自定义响应,但是对于使用路由模型绑定完成的查询,我该如何做到这一点?
这样的
或者我会坚持使用像这样的旧代码:
public function show($id)
{
$user = User::find($id);
if ( ! $user ) {
return $this->respondNotFound('User does not exist');
}
return $this->respond($this->userTransformer->transform($users));
}
所以我可以查看是否找不到资源(用户)并return适当的响应。
我认为 /app/Exceptions
Handler.php
文件是个好地方
public function render($request, Exception $e)
{
if ($e instanceof NotFoundHttpException) {
// return your custom response
}
return parent::render($request, $e);
}
看看你能不能抓到 ModelNotFound
。
public function render($request, Exception $e)
{
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
dd('model not found');
}
return parent::render($request, $e);
}
在 Laravel 7 和 8 中你可以这样做。
在 app/Exception/Handler.php class 中,添加如下所示的 render() 方法(如果不存在)。
请注意,您应该使用 Throwable .
而不是类型提示 Exception classuse Throwable;
public function render($request, Throwable $e)
{
if ($e instanceof \Illuminate\Database\Eloquent\ModelNotFoundException) {
//For API (json)
if (request()->wantsJson()) {
return response()->json([
'message' => 'Record Not Found !!!'
], 404);
}
//Normal
return view('PATH TO YOUR ERROR PAGE');
}
return parent::render($request, $e);
}