diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 2af91c6..d3c64fc 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,11 +3,13 @@ namespace App\Exceptions; use Illuminate\Auth\AuthenticationException; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Http\{Request, Response}; use Illuminate\Validation\ValidationException; use Overtrue\EasySms\Exceptions\NoGatewayAvailableException; use Slowlyo\OwlAdmin\Exceptions\AdminException; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; @@ -34,6 +36,18 @@ class Handler extends ExceptionHandler */ public function register(): void { + $this->map(ModelNotFoundException::class, function (ModelNotFoundException $e) { + $model = $e->getModel(); + + $resource = __($key = "model.{$model}"); + + if ($key === $resource) { + $resource = class_basename($model); + } + + return new HttpException(404, __(':resource not found', ['resource' => $resource]), $e); + }); + $this->renderable(function (RuntimeException $e, Request $request) { return response(['code' => $e->getCode(), 'message' => $e->getMessage()], $e->getHttpStatusCode()); }); diff --git a/lang/zh_CN.json b/lang/zh_CN.json new file mode 100644 index 0000000..47363b1 --- /dev/null +++ b/lang/zh_CN.json @@ -0,0 +1,3 @@ +{ + ":resource not found": ":resource 未找到" +} diff --git a/lang/zh_CN/model.php b/lang/zh_CN/model.php new file mode 100644 index 0000000..830e944 --- /dev/null +++ b/lang/zh_CN/model.php @@ -0,0 +1,21 @@ + '课件', + \App\Models\Train\Paper::class => '试卷', + \App\Models\Agreement::class => '合同', + \App\Models\Employee::class => '员工', + \App\Models\EmployeePromotion::class => '升职申请', + \App\Models\EmployeeSignRepair::class => '补卡申请', + \App\Models\HolidayApply::class => '请假申请', + \App\Models\OfficalBusiness::class => '出差报备', + \App\Models\OvertimeApply::class => '加班申请', + \App\Models\Reimbursement::class => '报销记录', + \App\Models\Store::class => '门店', + \App\Models\Task::class => '任务', + \App\Models\TaskHygiene::class => '任务', + \App\Models\TaskLedger::class => '任务', + \App\Models\TaskPerformance::class => '任务', + \App\Models\WorkflowCheck::class => '审核申请', + \App\Models\WorkflowLog::class => '审核记录', +];