mirror of
https://github.com/kuaifan/dootask.git
synced 2026-06-24 08:12:36 +00:00
- bootstrap/app.php 改为 Application::configure() 链式配置: withRouting(web/api/console) + withMiddleware + withExceptions - 删除 app/Http/Kernel.php、app/Console/Kernel.php:全局/分组中间件 归并到 13 默认栈,定制项经 trustProxies/trimStrings/ validateCsrfTokens/throttleApi/alias(webapi) 配置 API 表达 - 删除 app/Exceptions/Handler.php:ApiException/ModelNotFound 渲染、 ApiException 条件日志(report->stop)迁入 withExceptions; 图片动态裁剪逻辑抽为 App\Exceptions\ImagePathHandler - 删除 RouteServiceProvider/EventServiceProvider/AuthServiceProvider/ BroadcastServiceProvider:限流、14 个模型观察者、Registered 监听 迁入 AppServiceProvider::boot;新增 bootstrap/providers.php - 删除 7 个框架默认中间件子类(TrustProxies/TrimStrings/VerifyCsrfToken/ EncryptCookies/Authenticate/RedirectIfAuthenticated/ PreventRequestsDuringMaintenance)与未启用的 TrustHosts, 保留自定义 WebApi - config/app.php 移除 providers/aliases 数组(改用框架默认集 + bootstrap/providers.php,补齐 9~13 新增的框架 provider) - artisan、public/index.php 换 13 骨架版(handleCommand/handleRequest) 验证:LaravelS 正常拉起,/health、登录、token 认证、WebSocket 握手、 头像、裁剪(经 withExceptions)、404 兜底全过;php artisan test 145 passed/1 skipped;migrate:fresh 213 全过 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
76 lines
2.9 KiB
PHP
76 lines
2.9 KiB
PHP
<?php
|
||
|
||
use App\Exceptions\ApiException;
|
||
use App\Exceptions\ImagePathHandler;
|
||
use App\Module\Base;
|
||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||
use Illuminate\Foundation\Application;
|
||
use Illuminate\Foundation\Configuration\Exceptions;
|
||
use Illuminate\Foundation\Configuration\Middleware;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||
|
||
return Application::configure(basePath: $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__))
|
||
->withRouting(
|
||
web: __DIR__.'/../routes/web.php',
|
||
api: __DIR__.'/../routes/api.php',
|
||
apiPrefix: 'api',
|
||
commands: __DIR__.'/../routes/console.php',
|
||
)
|
||
->withMiddleware(function (Middleware $middleware): void {
|
||
// PHP(Swoole)只在内网被 nginx 访问,外部无法直连,故信任内网代理。
|
||
// 只采信 X-Forwarded-Proto:nginx 已用 $the_scheme 覆盖该头(值由 nginx 控制),
|
||
// 据此让 url() 实时跟随 https;host/for 一律不信,避免 Host 注入与 IP 伪造。
|
||
$middleware->trustProxies(at: '*', headers: Request::HEADER_X_FORWARDED_PROTO);
|
||
|
||
$middleware->trimStrings(except: [
|
||
'current_password',
|
||
'password',
|
||
'password_confirmation',
|
||
]);
|
||
|
||
$middleware->validateCsrfTokens(except: [
|
||
// 接口部分
|
||
'api/*',
|
||
|
||
// 发布桌面端
|
||
'desktop/publish/',
|
||
]);
|
||
|
||
// api 组限流(限流规则定义在 AppServiceProvider::boot)
|
||
$middleware->throttleApi();
|
||
|
||
$middleware->alias([
|
||
'webapi' => \App\Http\Middleware\WebApi::class,
|
||
]);
|
||
|
||
$middleware->redirectGuestsTo('/login');
|
||
$middleware->redirectUsersTo('/home');
|
||
})
|
||
->withExceptions(function (Exceptions $exceptions): void {
|
||
// /uploads/**.png/crop/... 动态裁剪与缩略图(命中则返回图片,否则走默认 404)
|
||
$exceptions->render(function (NotFoundHttpException $e, Request $request) {
|
||
return ImagePathHandler::render($request);
|
||
});
|
||
|
||
$exceptions->render(function (ApiException $e) {
|
||
return response()->json(Base::retError($e->getMessage(), $e->getData(), $e->getCode()));
|
||
});
|
||
|
||
$exceptions->render(function (ModelNotFoundException $e) {
|
||
return response()->json(Base::retError('Interface error'));
|
||
});
|
||
|
||
// ApiException 按 isWriteLog 决定是否记录,且不走默认 report
|
||
$exceptions->report(function (ApiException $e) {
|
||
if ($e->isWriteLog()) {
|
||
Log::error($e->getMessage(), [
|
||
'code' => $e->getCode(),
|
||
'data' => $e->getData(),
|
||
'exception' => ' at ' . $e->getFile() . ':' . $e->getLine()
|
||
]);
|
||
}
|
||
})->stop();
|
||
})->create();
|