|
1 | 1 | # PHP QuickORM 框架开发文档
|
2 | 2 |
|
3 |
| -版本:20180906 |
| 3 | +版本:20180911 |
4 | 4 |
|
5 | 5 | ## 简介
|
6 | 6 |
|
@@ -157,11 +157,182 @@ PHP QuickORM 框架的路由系统采用的是请求地址与控制器方法一
|
157 | 157 |
|
158 | 158 | ## 请求
|
159 | 159 |
|
160 |
| -TODO |
| 160 | +PHP QuickORM 框架针对每一次请求,都会生成一个 Request 类的对象,在开发过程中关于请求的信息均可从中获取。 |
| 161 | + |
| 162 | +### 请求内容 |
| 163 | + |
| 164 | +#### 全部字段 |
| 165 | + |
| 166 | +获取请求的全部字段,包括但不限定 `GET` 、`POST` 等方法。 |
| 167 | +返回为 PHP `Array` 数组,在 Controller 控制器层的操作具体如下: |
| 168 | + |
| 169 | +```php |
| 170 | +// 假设此时访问 /?info=test |
| 171 | + |
| 172 | +$content = $this->request()->all(); // $content['info'] = test |
| 173 | +``` |
| 174 | + |
| 175 | +#### 单个字段 |
| 176 | + |
| 177 | +获取请求中某个字段的内容,包括但不限定 `GET` 、`POST` 等方法。 |
| 178 | +返回为字符串或数组,且对字段的获取与请求方法无关。在 Controller 控制器层的操作具体如下: |
| 179 | +```php |
| 180 | +// 假设此时以 GET 方式访问 /?info=test |
| 181 | + |
| 182 | +$content = $this->request('info'); // test |
| 183 | + |
| 184 | +// 假设此时以 POST 方式发送 user=demo |
| 185 | + |
| 186 | +$content = $this->request('user'); // demo |
| 187 | + |
| 188 | +// 假设此时以 DELETE 方式发送 user=demo 到 /?info=test |
| 189 | + |
| 190 | +$content = $this->request('user'); // demo |
| 191 | +$content = $this->request('info'); // test |
| 192 | + |
| 193 | +// 以下几种方法效果一致,可根据习惯使用: |
| 194 | +$content = $this->request()->get('user'); // demo |
| 195 | +$content = $this->request()->get('info'); // test |
| 196 | +``` |
| 197 | + |
| 198 | +### 请求方法 |
| 199 | + |
| 200 | +获取请求的方法。具体如下: |
| 201 | + |
| 202 | +```php |
| 203 | +$method = $this->request()->getMethod(); |
| 204 | +``` |
| 205 | + |
| 206 | +### 请求路径 |
| 207 | + |
| 208 | +#### 目录地址 |
| 209 | + |
| 210 | +获取返回的目录地址。具体如下: |
| 211 | + |
| 212 | +```php |
| 213 | +// 假设访问 /hello/word/?info=test |
| 214 | +$method = $this->request()->getPath(); // /hello/word/ |
| 215 | +``` |
| 216 | + |
| 217 | +#### 请求链接 |
| 218 | +获取返回的请求链接。具体如下: |
| 219 | + |
| 220 | +```php |
| 221 | +// 假设访问 /hello/word/?info=test |
| 222 | +$method = $this->request()->getUrl(); // /hello/word/?info=test |
| 223 | +``` |
161 | 224 |
|
162 | 225 | ## 响应
|
163 | 226 |
|
164 |
| -TODO |
| 227 | +在项目开发过程中,针对每一个请求,都必须做出合理的响应。作为针对 `MVVM` 架构而生的 PHP QuickORM,响应方法如下。 |
| 228 | + |
| 229 | +### JSON 数据 |
| 230 | + |
| 231 | +直接返回 JSON 格式的数据,并返回 HTTP 状态码。具体如下: |
| 232 | + |
| 233 | +```php |
| 234 | +$data = ["text" => "hello word"]; |
| 235 | + |
| 236 | +// 直接返回 JSON 格式的数据,默认 HTTP 状态码为 200 |
| 237 | +return $this->response()->json($data); |
| 238 | + |
| 239 | +// 直接返回 JSON 格式的数据,并设置 HTTP 状态码为 404 |
| 240 | +return $this->response()->json($data, '404'); |
| 241 | +``` |
| 242 | + |
| 243 | +### JSON 对象 |
| 244 | + |
| 245 | +返回 JSON 格式的对象,包含 `errcode`、`errmsg`、`data` 三个属性,常用于使前后端交互更加规范化,具体如下: |
| 246 | + |
| 247 | +```php |
| 248 | + |
| 249 | +$data = ["text" => "hello word"]; |
| 250 | + |
| 251 | +// 返回 JSON 对象,默认 HTTP 状态码为 200,errcode 为 0,errmsg 为空 |
| 252 | +return $this->response($data); |
| 253 | + |
| 254 | +// 返回 JSON 对象,并设置 HTTP 状态码为 404,且保持默认的 errcode 为 0,errmsg 为空 |
| 255 | +return $this->response($data, '404'); |
| 256 | + |
| 257 | +// 返回 JSON 对象,并设置 HTTP 状态码为 404,并设置 errcode 为 500001,errmsg 为 hello |
| 258 | +return $this->response($data, '404', '500001', ); |
| 259 | + |
| 260 | +// 以下几种方法效果一致,可根据习惯使用 |
| 261 | +return $this->response()->dataEcode($data); |
| 262 | +return $this->response()->dataEcode($data, '404'); |
| 263 | +return $this->response()->dataEcode($data, '404', '500001', ); |
| 264 | + |
| 265 | +// JSON 对象示例 |
| 266 | +// { |
| 267 | +// "errcode": "0", |
| 268 | +// "errmsg": "null", |
| 269 | +// "data": { |
| 270 | +// "text": "hello word" |
| 271 | +// } |
| 272 | +// } |
| 273 | +``` |
| 274 | + |
| 275 | +若您的数据调用了 `paginate()` 方法实现分页,则应该使用 JSON 分页对象,具体表现为在返回对象增加了 `page` 属性,如下: |
| 276 | + |
| 277 | +```php |
| 278 | +$data = Demo::piginate(3); |
| 279 | + |
| 280 | +// 返回 JSON 对象,默认 HTTP 状态码为 200,errcode 为 0,errmsg 为空 |
| 281 | +return $this->response()->pageEncode($data); |
| 282 | + |
| 283 | +// JSON 分页对象示例 |
| 284 | +// { |
| 285 | +// "errcode": 0, |
| 286 | +// "errmsg": "", |
| 287 | +// "data": [{ |
| 288 | +// "id": "1", |
| 289 | +// "title": "嘿!Every Body @~@", |
| 290 | +// "content": "测试的内容!!", |
| 291 | +// "author": "3" |
| 292 | +// }, { |
| 293 | +// "id": "2", |
| 294 | +// "title": "还是标题", |
| 295 | +// "content": "这是无敌的测试", |
| 296 | +// "author": "Rytia" |
| 297 | +// }, { |
| 298 | +// "id": "4", |
| 299 | +// "title": "标题啦啦", |
| 300 | +// "content": "这是依旧是无敌的测试", |
| 301 | +// "author": "Rytia" |
| 302 | +// }], |
| 303 | +// "page": { |
| 304 | +// "currentPage": 1, |
| 305 | +// "totalItems": 14, |
| 306 | +// "totalPages": 5, |
| 307 | +// "hasNext": "true", |
| 308 | +// "nextUrl": "\/api\/test\/1?page=2" |
| 309 | +// } |
| 310 | +// } |
| 311 | +``` |
| 312 | + |
| 313 | +### 重定向 |
| 314 | + |
| 315 | +使用 302 重定向至其他链接,具体如下: |
| 316 | + |
| 317 | +```php |
| 318 | +return $this->response()->redirect("http://www.github.com/php-quickorm"); |
| 319 | +``` |
| 320 | + |
| 321 | +### 响应头部 |
| 322 | + |
| 323 | +用于响应请求时,增加额外的 `responose header` 信息,支持链式操作。具体如下: |
| 324 | + |
| 325 | +```php |
| 326 | +$data = ["text" => "hello word"]; |
| 327 | + |
| 328 | +// 分步操作 |
| 329 | +$this->response()->setHeader("Powered_By", "php-quickorm"); |
| 330 | +return $this->response()->json($data); |
| 331 | + |
| 332 | +// 链式操作 |
| 333 | +return $this->response()->setHeader("Powered_By", "php-quickorm")->json($data); |
| 334 | + |
| 335 | +``` |
165 | 336 |
|
166 | 337 | ## 数据库
|
167 | 338 |
|
@@ -690,10 +861,10 @@ $collection->sort();
|
690 | 861 | $collection->sort("DESC");
|
691 | 862 |
|
692 | 863 | // 采用其他 PHP 排序方法,这里使用 ksort() 为例
|
693 |
| -$collection->("DESC", "ksort"); |
| 864 | +$collection->sort("DESC", "ksort"); |
694 | 865 |
|
695 | 866 | // 采用其他需要回调函数的 PHP 排序方法,这里以 usort() 为例
|
696 |
| -$collection->("", "usort", function($a, $b){ |
| 867 | +$collection->sort("", "usort", function($a, $b){ |
697 | 868 | // 排序函数主体
|
698 | 869 | });
|
699 | 870 |
|
@@ -1075,5 +1246,3 @@ $result = $collection->toJson();
|
1075 | 1246 |
|
1076 | 1247 | 同时 `Collection` 另有 `toArray()` 方法可以将集合转换为普通 PHP 数组,具体参照本文档相对于章节。
|
1077 | 1248 |
|
1078 |
| - |
1079 |
| - |
|
0 commit comments