withRouting( web: __DIR__.'/../routes/web.php', api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', health: '/up', then: function () { // 注册后台管理路由,使用admin前缀,使用Token认证 Route::prefix('admin') ->group(base_path('routes/admin.php')); }, ) ->withMiddleware(function (Middleware $middleware): void { // 注册自定义API认证中间件 $middleware->alias([ 'admin.auth' => \App\Http\Middleware\AdminApiAuthenticate::class, ]); }) ->withExceptions(function (Exceptions $exceptions): void { // 配置不需要报告的异常类型 $exceptions->dontReport([ \App\Exceptions\BusinessException::class, ]); // 全局API异常处理 - 所有admin路由和期望JSON的请求 $exceptions->render(function (Throwable $e, $request) { // 只处理admin路由和API请求 if ($request->expectsJson() || $request->is('admin/*')) { // 让Handler类处理JSON响应 $handler = app(\App\Exceptions\Handler::class); return $handler->render($request, $e); } }); })->create();