本文目录导读:

我来创建一个PHP项目模块依赖图的示例,展示一个典型的MVC架构项目的模块关系。
文本形式的依赖图
// 项目模块依赖关系图 +------------------------------------------------------------------+ | PHP Project (MVC) | +------------------------------------------------------------------+ | | | +-------------------+ +-------------------+ | | | 前端控制器 |----->| 路由模块 | | | | (FrontController) | | (Router) | | | +-------------------+ +-------------------+ | | | | | | v v | | +-------------------+ +-------------------+ | | | 中间件层 |<-----| 请求/响应处理 | | | | (Middleware) | | (Request) | | | +-------------------+ +-------------------+ | | | | | v | | +-------------------+ +-------------------+ | | | 控制器 |----->| 服务层 | | | | (Controller) | | (Service) | | | +-------------------+ +-------------------+ | | | | | | v v | | +-------------------+ +-------------------+ | | | 视图层 |<-----| 模型层 | | | | (View/Template) | | (Model) | | | +-------------------+ +-------------------+ | | | | | | v v | | +-------------------+ +-------------------+ | | | 模板引擎 | | 数据访问层 | | | | (Template) | | (Repository) | | | +-------------------+ +-------------------+ | | | | | v | | +-------------------+ | | | 数据库 | | | | (Database) | | | +-------------------+ | +------------------------------------------------------------------+
Mermaid格式依赖图
graph TD
subgraph "前端层 (Frontend)"
A[用户请求] --> B[前端控制器 FrontController]
B -->|解析请求| C[路由器 Router]
end
subgraph "中间层 (Middleware)"
C --> D[中间件链 Middleware Chain]
D --> E[认证中间件 Auth]
D --> F[日志中间件 Logging]
D --> G[CSRF保护 CSRF]
D --> H[跨域处理 CORS]
end
subgraph "应用层 (Application)"
H --> I[控制器层 Controller]
I --> J[服务层 Service]
J --> K[业务逻辑层 Business Logic]
end
subgraph "数据层 (Data)"
J --> L[模型层 Model]
L --> M[数据映射层 Repository]
M --> N[(数据库 Database)]
end
subgraph "视图层 (View)"
I --> O[视图 View]
O --> P[模板引擎 Template Engine]
P --> Q[HTML/CSS/JS 输出]
end
%% 辅助模块
subgraph "辅助模块 (Support)"
R[配置管理 Config]
S[缓存模块 Cache]
T[日志模块 Logging]
U[异常处理 Exception]
V[验证模块 Validation]
W[安全模块 Security]
end
%% 依赖关系
R --> I
R --> J
S --> J
T --> D
U --> I
V --> I
W --> D
W --> J
完整项目结构示例
project-root/
├── public/ # Web根目录
│ ├── index.php # 前端控制器
│ └── assets/ # 静态资源
├── app/
│ ├── Controllers/ # 控制器层
│ │ ├── HomeController.php
│ │ ├── UserController.php
│ │ └── ApiController.php
│ ├── Middleware/ # 中间件层
│ │ ├── AuthMiddleware.php
│ │ ├── CorsMiddleware.php
│ │ └── LogMiddleware.php
│ ├── Services/ # 服务层
│ │ ├── UserService.php
│ │ ├── OrderService.php
│ │ └── PaymentService.php
│ ├── Repositories/ # 数据仓储层
│ │ ├── UserRepository.php
│ │ └── OrderRepository.php
│ └── Models/ # 模型层
│ ├── User.php
│ └── Order.php
├── config/ # 配置目录
│ ├── database.php
│ ├── app.php
│ └── routes.php
├── routes/ # 路由定义
│ ├── web.php
│ └── api.php
├── resources/
│ ├── views/ # 视图模板
│ │ ├── home/
│ │ └── layouts/
│ └── lang/ # 语言文件
├── storage/ # 存储目录
│ ├── logs/
│ ├── cache/
│ └── uploads/
├── vendor/ # Composer依赖
├── tests/ # 测试目录
├── composer.json
└── .env # 环境配置
依赖关系说明
1 核心依赖方向
// 依赖方向图(低层模块不依赖高层)
+----------------+
| 控制器层 |
| (Controllers) |
+--------+-------+
|
| 依赖
v
+----------------+
| 服务层 |
| (Services) |
+--------+-------+
|
| 依赖
v
+----------------+
| 仓储层 |
| (Repositories) |
+--------+-------+
|
| 依赖
v
+----------------+
| 模型/数据库 |
| (Models/DB) |
+----------------+
2 关键依赖规则
// 依赖原则示例
class UserController
{
private $userService;
// 控制器依赖服务层接口
public function __construct(UserServiceInterface $userService)
{
$this->userService = $userService;
}
}
class UserService
{
private $userRepository;
// 服务层依赖仓储层接口
public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
}
class UserRepository
{
private $db;
// 仓储层依赖数据库连接
public function __construct(DatabaseInterface $db)
{
$this->db = $db;
}
}
3 依赖注入容器
// 依赖注入容器配置
$container = new Container();
// 注册服务
$container->set(UserRepositoryInterface::class, UserRepository::class);
$container->set(UserServiceInterface::class, function($c) {
return new UserService($c->get(UserRepositoryInterface::class));
});
$container->set(UserController::class, function($c) {
return new UserController($c->get(UserServiceInterface::class));
});
常见框架的模块依赖(以Laravel为例)
graph LR
subgraph "Laravel框架"
A[Route Service] --> B[Controller]
B --> C[Model]
B --> D[View]
C --> E[Migration]
C --> F[Seeder]
G[Request] --> B
H[Response] --> B
I[Validation] --> B
J[Auth] --> B
K[Event/Listener] --> B
B --> L[Service Provider]
L --> M[Container]
M --> N[Facade]
end
最佳实践建议
1 依赖方向原则
// ✅ 正确 - 高层依赖低层 Controller → Service → Repository → Model // ❌ 避免 - 循环依赖 Model → Controller → Model → Repository → Model
2 模块解耦示例
// 使用接口解耦模块依赖
interface PaymentGatewayInterface
{
public function process(Order $order): bool;
}
class StripePayment implements PaymentGatewayInterface {}
class PaypalPayment implements PaymentGatewayInterface {}
// 依赖接口而非具体实现
class OrderService
{
private $paymentGateway;
public function __construct(PaymentGatewayInterface $paymentGateway)
{
$this->paymentGateway = $paymentGateway;
}
}
3 依赖检测工具
# 使用工具检测循环依赖 composer require phpmd/phpmd vendor/bin/phpmd src text cleancode,codesize,controversial,design # 使用PHPStan检查类型安全 composer require --dev phpstan/phpstan vendor/bin/phpstan analyse src --level=max
这个依赖图展示了PHP项目各模块之间的典型关系,核心原则是:
- 单向依赖:高层模块依赖低层模块
- 接口隔离:通过接口解耦模块
- 依赖注入:通过依赖注入管理对象创建
- 模块独立:各模块可以独立开发测试