51 lines
1.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;
return Application::configure(basePath: dirname(__DIR__))
->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,
]);
// 添加CORS中间件到全局中间件组
$middleware->web(append: [
\Illuminate\Http\Middleware\HandleCors::class,
]);
$middleware->api(append: [
\Illuminate\Http\Middleware\HandleCors::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();