src/
├── Domain/ # 核心業務邏輯,零外部依賴
│ ├── Entity/ # 富領域模型
│ ├── Repository/ # 介面定義
│ └── Service/ # 領域服務
├── Application/ # 用例協調器
│ ├── Command/ # 寫操作
│ └── Query/ # 讀操作 (CQRS)
├── Infrastructure/ # 技術實作
│ ├── Persistence/ # Doctrine/PDO Repository 實作
│ ├── Http/ # Controller、Middleware
│ └── Messaging/ # Queue、Event Bus
└── Interface/ # 進程入口 (Console, Web, gRPC)
依賴方向:Interface → Application → Domain ← Infrastructure (依賴倒置)。
<?php
// 使用 PHP-DI 定義
return [
UserRepositoryInterface::class => \DI\autowire(UserRepositoryDoctrine::class),
UserService::class => \DI\autowire()->constructorParameter('mailer', \DI\get(MailerInterface::class)),
// Controller 自動注入
];
// Controller
class UserController {
public function __construct(private UserService $service) {}
public function show(int $id): JsonResponse {
return new JsonResponse($this->service->getUser($id));
}
}
?>
class UserNotFoundException extends DomainException。Result::success($data) / Result::failure($code, $message)。{"error": {"code": "...", "message": "..."}})。vlucas/phpdotenv + symfony/config 驗證配置完整性。遵循這些實踐,專案將具備清晰邊界、低耦合、高可測試性,支撐長期演進。