有谁知道如何使用 Json 格式的响应以及相应的 HTTP 错误和发生错误的描述性消息,使应用程序以一般方式返回任何异常,而不执行任何类型的重定向到 发生时间?
这是我想要的一个例子
{
"errors": [],
"exception":
"Illuminate\\Database\\Eloquent\\ModelNotFoundException",
"file":
"/var/www/project/vendor/laravel/framework/src/Illuminate/Database/Eloqu
ent/Builder.php",
"line": 598,
"message": "No query results for model
[App\\Models\\API\\ModelName].",
"statusCode": 404
}
我试图从 App/Exception/Handler.php 执行此操作
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* The list of the inputs that are never flashed to the session on validation exceptions.
*
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
$this->reportable(function (Throwable $e) {
$Status = array('msg'=>$e);
return response ($Status,$e);
});
}
}