-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityController.php
More file actions
212 lines (202 loc) · 5.68 KB
/
EntityController.php
File metadata and controls
212 lines (202 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
// 实体类规范查看 App/Entity/User.php
namespace App\Controller;
use App\Entity\Nested\Company;
use App\Entity\Nested\Employee;
use App\Entity\Info;
use App\Entity\User;
/**
* 请求数据绑定实体类
*
* @path /entity
*/
class EntityController
{
/**
* demo1
*
* @route POST /demo1
* @param \App\Entity\User $user
*/
public function demo1(User $user)
{
// 客户端提交数据:
// {
// "user": {
// "id": 1
// "name": "jack",
// "age": 18
// }
// }
//
// 绑定实体类可以分别在两个地方指定
// 1. function(实体类型 $xxx)
// 2. @param 实体类型 $xxx
//
// 任何一处指定后, 方法参数都为一个实体类型
// 否则, 虽然也能绑定request 数据, 但得到的是一个关联数组
//
// 推荐使用方法 1. 绑定, 当然更推荐两个地方都写,增加代码可读性 :)
// 因为直接在 function 上指定, PHP语言特性会直接识别为一个实体类,
// 不会再解析 @param 中的内容, @param 中写不写, 或写不写全命名空间都无所谓, 只会判断类型是否一至
// 否则若只在 @param 中指定的话, 会多执行一个解析过程
//
// 如果只在 @param 中指定,也推荐写全命名空间, 这样只有一个判断 class_exists('\Entity\User')
// 否则如果只写 User, 还会多一个结合上下文的反射命名空间过程
return $user;
}
/**
* demo2
*
* @route POST /demo2
* @param $user
*/
public function demo2($user)
{
// 客户端提交数据:
// {
// "user": {
// "id": 1
// "name": "jack",
// "age": 18
// }
// }
//
// 两处都不指定实体类也可以得到request数据,
// 但user只是个array, 并不是一个实体类
// 同时也无法使用框架验证规则,只能在业务代码里验证
\PhpRest\dump($user);
return 'OK';
}
/**
* demo3
*
* @route POST /demo3
* @param \App\Entity\User $user {@bind request.user}
* @param \App\Entity\Info $info {@bind request.info}
*/
public function demo3(User $user, Info $info)
{
// 客户端提交数据:
// {
// "user": {
// "id": 1
// "name": "jack",
// "age": 18
// },
// "info": {
// "id": 1
// "email": "xxx"
// }
// }
//
// 因为默认 @bind request.xxx 同名参数名
// 所以 {@bind request.user} 写不写都一样
// 除非 @param $dog {@bind request.user}
\PhpRest\dump($user);
\PhpRest\dump($info);
return 'OK';
}
/**
* demo4
*
* 实体类嵌套实体类, 嵌套实体类数组, 嵌套基础类型数组
*
* @route POST /demo4
* @param \App\Entity\Nested\Company $company
*/
public function demo4(Company $company)
{
// 客户端提交数据:
// {
// "company": {
// "id": 1,
// "name": "xxxx公司",
// "employee": {
// "id": 1
// "realName": "jack",
// "companyId": 18
// },
// "order": {
// "id": 1,
// "code": "1231214351",
// "orderInfo": {
// "id": 123,
// "desc": "info desc"
// },
// "orderOthers": [
// {
// "id": 1,
// "ips": ['127.0.0.1', '127.0.0.2', '127.0.0.3'],
// "nums": [1,2,3],
// },
// {
// "id": 2,
// "ips": ['127.0.1.1', '127.0.2.2'],
// "nums": [1,2,3,4,5,6],
// }
// ]
// }
// }
// }
//
\PhpRest\dump($company);
return 'OK';
}
/**
* demo5
*
* 绑定到实体类数组, 使用场景通常应该都是POST吧
*
* @route POST /demo5
* @param \App\Entity\User[] $users
*/
public function demo5($users)
{
// 客户端提交数据:
// {
// "users": [
// {
// "id": 1,
// "name": "aaa",
// "age": 16
// },
// {
// "id": 2,
// "name": "bbb",
// "age": 17
// },
// {
// "id": 3,
// "name": "ccc",
// "age": 18
// }
// ]
// }
//
\PhpRest\dump($users);
return 'OK';
}
/**
* demo6
*
* 实体类支持继承 ObjSon 继承于 ObjFather
*
* @route POST /demo6
* @param \App\Entity\Inherit\ObjSon $son
*/
public function demo6($son)
{
// 客户端提交数据:
// {
// "son": {
// "id": 1,
// "name": "aaa",
// "age": 16
// }
// }
//
\PhpRest\dump($son);
return 'OK';
}
}