Skip to content

Commit 018e248

Browse files
committed
完善 System路径, 发布 v1.0
1 parent 6d89ee6 commit 018e248

File tree

19 files changed

+81
-48
lines changed

19 files changed

+81
-48
lines changed

App/Controller/apiController.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,83 @@ class apiController extends Controller {
99

1010
public function testGet($id){
1111
$data = Demo::find($id);
12-
return $this->response();
12+
return $this->response($data);
1313
}
1414

1515
// ORM 查询类方法演示
1616

17-
public function find($id){
17+
public function findGet($id){
1818
// 通过 id 查询数据表
1919
$resultObject = Demo::find($id);
20-
dd($resultObject);
20+
return $this->response($resultObject);
2121
}
2222

2323
public function whereGet(){
2424
// 条件检索数据表(条件数组)
2525
$conditionArray = ["author" => "Rytia"];
2626
$resultObjectArray = Demo::where($conditionArray)->get();
27-
dd($resultObjectArray);
27+
return $this->response($resultObjectArray);
2828
}
2929

3030
public function whereRawGet(){
3131
// 条件检索数据表(SQL语句)
3232
$conditionStatement = 'author LIKE "Rytia"';
3333
$resultObjectArray = Demo::whereRaw($conditionStatement)->get();
34-
dd($resultObjectArray);
34+
return $this->response($resultObjectArray);
3535
}
3636

3737
public function allGet(){
3838
// 显示全部数据
3939
$resultObjectArray = Demo::all();
40-
dd($resultObjectArray);
40+
return $this->response($resultObjectArray);
4141
}
4242

4343
public function rawGet(){
4444
// 执行SQL语句
4545
$sqlStatement = 'SELECT * FROM {table} WHERE author LIKE "Rytia"';
4646
$resultObjectArray = Demo::raw($sqlStatement)->get();
4747
dd($resultObjectArray);
48+
return $this->response($resultObjectArray);
4849
}
4950

5051
public function searchGet(){
5152
// 数据表字段搜索
5253
$resultObjectArray = Demo::search("title", "%嗯%");
53-
dd($resultObjectArray);
54+
return $this->response($resultObjectArray);
5455
}
5556

5657
public function orWhereGet(){
5758
// 多重条件检索数据表(条件数组)
5859
// 支持 where、whereRaw、orWhere、orWhereRaw
5960
$test = Demo::where(['title' => '还是标题'])->orWhere(['title' => '测试标题'])->get();
60-
dd($test);
61+
return $this->response($test);
6162
}
6263

6364
public function deleteGet($id){
6465
// 删除条目
6566
$result = Demo::find($id)->delete();
66-
dd($result);
67+
return $this->response($result);
6768
}
6869

69-
public function paginateGet($pageNum){
70+
public function paginateGet(){
7071
// 条目分页演示
7172

7273
// 直接调用:相当于 Database 层分页,效率高
73-
$test = Demo::paginate(3);
74+
$test = Demo::paginate(5);
7475

7576
// Collection 层分页:先把全部数据取出再通过 Collection 分页,效率低
76-
$test = Demo::all()->paginate(3);
77+
$test = Demo::all()->paginate(5);
7778

7879
// Database 层分页:在 SQL 语句里添加 LIMIT,效率高
79-
$test = Demo::where()->paginate(3);
80-
$test = Database::table('demo')->where()->setModel(Demo::class)->paginate(3);
81-
$test = Database::model(Demo::class)->where()->paginate(3);
80+
$test = Demo::where()->paginate(5);
81+
$test = Database::table('demo')->where()->setModel(Demo::class)->paginate(5);
82+
$test = Database::model(Demo::class)->where()->paginate(5);
83+
84+
// 直接返回(前端无法判断分页,不推荐)
85+
// return $this->response($test);
8286

83-
dd($test);
87+
// 采用分页专用对象返回(带有 page 字段供前端使用,推荐)
88+
return $this->response()->pageEncode($test);
8489
}
8590

8691
public function databaseWhereGet(){
@@ -92,7 +97,7 @@ public function databaseWhereGet(){
9297
->orWhereRaw('content LIKE "%测试%"')
9398
->paginate(5);
9499

95-
dd($result);
100+
return $this->response($result);
96101

97102
}
98103

@@ -104,7 +109,7 @@ public function databaseOnGet(){
104109
->on('wiki.zhong=zhong.id')
105110
->orderBy("coordinate", "DESC")
106111
->fetchAll();
107-
dd($result);
112+
return $this->response($result);
108113

109114
}
110115

Public/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// 如果您已将产品真正投入使用,请将您的工作目录设置为 /Public,方便与静态文件的统一管理
1616

1717
// 启动相应函数
18-
require_once __DIR__ . "/App/System/Utility/Functions.php";
18+
require_once __DIR__ . "/../System/Utility/Functions.php";
1919

2020
// 设置错误与异常处理
2121
set_error_handler('\System\Utility\Exception::errorReport');

App/System/Collection.php renamed to System/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @copyright 2018 PHP-QuickORM
1212
*/
1313

14-
class Collection implements ArrayAccess, Countable, IteratorAggregate
14+
class Collection implements ArrayAccess, Countable, IteratorAggregate, Jsonable
1515
{
1616

1717
protected $collectionItems = [];
@@ -338,7 +338,7 @@ public function forPage($pageNum, $currentPage, $total, $furtherPageInfo = true)
338338
// 添加分页的属性
339339
$this->collectionPages = new \stdClass();
340340
$this->collectionPages->currentPage = $currentPage;
341-
$this->collectionPages->totalItems = $total;
341+
$this->collectionPages->totalItems = intval($total);
342342
$this->collectionPages->totalPages = ceil($total / $pageNum);
343343
$this->collectionPages->hasNext = ($this->collectionPages->totalPages > $currentPage) ? "true" : "false";
344344
$this->collectionPages->nextUrl =($this->collectionPages->hasNext == "true") ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH).'?page='.($currentPage+1) : NULL;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

App/System/Http/Response.php renamed to System/Http/Response.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function back(){
6565
* @param int $errcode
6666
* @param string $errmsg
6767
*/
68-
public function dataEncode($data, $statusCode, $errcode = 0, $errmsg = ''){
68+
public function dataEncode($data, $statusCode = 200, $errcode = 0, $errmsg = ''){
6969
// 同理,如果实现了 Jsonable 则先反转,这里是的简化版
7070
$data = ($data instanceof Jsonable) ? json_decode($data) : $data;
7171

@@ -78,6 +78,32 @@ public function dataEncode($data, $statusCode, $errcode = 0, $errmsg = ''){
7878
$this->json($result,$statusCode);
7979
}
8080

81+
/**
82+
* 将 Collection 集合以标准 JSON 对象返回并附带分页信息
83+
* @param $data
84+
* @param $statusCode
85+
* @param int $errcode
86+
* @param string $errmsg
87+
*/
88+
public function pageEncode($data, $statusCode = 200, $errcode = 0, $errmsg = ''){
89+
90+
// 判断是否可分页(Collection对象)
91+
if(!isset($data->collectionPages) || empty($data->collectionPages)){
92+
trigger_error("data isn't the instance of Paginate", E_USER_ERROR);
93+
}
94+
95+
// 将 Collection 自身的 collectionPages 合并到对象并返回
96+
$result = [
97+
'errcode' => $errcode,
98+
'errmsg' => $errmsg,
99+
'data' => json_decode($data),
100+
'page' => $data->collectionPages
101+
];
102+
103+
$this->json($result,$statusCode);
104+
}
105+
106+
81107
/**
82108
* 响应头部设置
83109
* @param string $key
File renamed without changes.

0 commit comments

Comments
 (0)