AddFeature: 处理页面未找到错误自动返回404

重写 render 方法
添加 register 方法的 void 返回值

Signed-off-by: XiaoLFeng <gm@x-lf.cn>
This commit is contained in:
筱锋xiao_lfeng 2023-07-09 15:41:04 +08:00
parent 8a52e35c94
commit f4de7d71bd

View File

@ -7,7 +7,13 @@
namespace App\Exceptions; namespace App\Exceptions;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable; use Throwable;
class Handler extends ExceptionHandler class Handler extends ExceptionHandler
@ -37,10 +43,26 @@ class Handler extends ExceptionHandler
* *
* @return void * @return void
*/ */
public function register() public function register(): void
{ {
$this->reportable(function (Throwable $e) { $this->reportable(function (Throwable $e) {
// //
}); });
} }
/**
* 处理发现页面找不到之后返回内容
*
* @param Request $request 获取请求内容
* @param Throwable $e 抛出错误
* @return Response|JsonResponse|\Symfony\Component\HttpFoundation\Response|RedirectResponse
* @throws Throwable
*/
public function render($request, Throwable $e): Response|JsonResponse|\Symfony\Component\HttpFoundation\Response|RedirectResponse
{
if ($e instanceof ModelNotFoundException || $e instanceof NotFoundHttpException) {
return response()->redirectToRoute('404');
}
return parent::render($request, $e);
}
} }