vd/documents/instructions.md
2018-11-05 09:26:30 +08:00

120 lines
3.2 KiB
Markdown
Raw 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.

## 项目结构
### `arch` 目录
`arch` 将业务代码与框架代码分离,主要实现一下几点功能:
- 启动服务加载
- 逻辑分层基类
- 统一输出处理
- 统一错误处理
- 自动缓存处理
- 标准查询能力
- 代码生成器
- 数据库能力扩展
- Request能力扩展
- HTTP请求/数据库查询 Debug
- 一些辅助函数
### `app` 目录
`app` 内存放业务逻辑代码,有以下几个目录:
#### [ `Console` 目录]
命令行程序的核心引导文件;
#### [ `Core` 目录]
一些核心的基类放在这儿,供其他类继承调用;
#### [ `Events` 目录]
公用事件,事件应当是公用的,而监听是各自的;
### [ `Exceptions` 目录]
错误类的定义,如有需要自定义错误码等,请在该目录定义;
### [ `Models` 目录]
模型层的定义业务应围绕数据进行展开数据是公用的而Repository仓库是各自的
### [ `Domains` 目录]
以功能点为领域进行开发,这是本项目结构的核心部分;旨在进行低耦合的开发,明确各个功能细分;
`Domains` 文件结构:
```
Domains 1
├── Console
│── Database
│ └── migrations
│── Http
│ ├── Controllers
│ ├── Middleware
│ └── Requests
├── Providers
├── Repositories
├── Routes
├── Services
└── Tests
└── Services
Domains 2
├── Console
│── Database
│ └── migrations
│── Http
│ ├── Controllers
│ ├── Middleware
│ └── Requests
├── Providers
├── Repositories
├── Routes
├── Services
└── Tests
└── Services
```
> 生成命令: `php dipper make:domain {Name}`
#### 请求生命周期:
Route -> Request -> Controller -> Service -> Repository -> Model
#### 路由 Route
生成的Domain中会默认含有两个路由文件 api.php 和 web.php。
api.php 会有全局的json输出处理所有的api接口都应该放在这个目录。
web.php 适合书写页面文件的路由。
- 注意路由的一个前缀建议为该Domain名称的 复数形式。
#### 请求Request
- `Request` 可以放表单验证/身份认证 规则。
- `Request` 只能在控制器里被注入使用,在注入时自动运行验证规则,如果不通过就立即抛出异常。
> 生成命令: `php dipper make:request {RequestName} [DomainName]`
#### 控制器Controller
- 尽量不要将业务逻辑写在Controller层放在Service层可以得到更好的复用。
> 生成命令: `php dipper make:controller {ControllerName} [DomainName]`
#### 服务层Service
主要书写业务逻辑的地方放在Service层可以得到更好的复用。如一个功能同时拥有后台和手机端等是可以公用一个Service来查询数据的。
> 生成命令: `php dipper make:service {ServiceName} [DomainName]`
#### 仓库层Repository
主要书写数据库查询的地方,一些复杂的查询应该写这里,方便后期做查询索引等优化。
> 生成命令: `php dipper make:repository {RepositoryName} [DomainName]`