Skip to content

Commit db25013

Browse files
committed
完善 路由系统
1 parent af718d3 commit db25013

File tree

11 files changed

+297
-136
lines changed

11 files changed

+297
-136
lines changed

.idea/workspace.xml

Lines changed: 115 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

App/Controller/apiController.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
class apiController extends Controller {
99

10-
public function test($id){
11-
10+
public function testGet($id){
11+
header("X-Powered-By: PHP-QuickORM");
12+
return $this->response()->json(Demo::find($id));
1213
}
1314

1415
// ORM 查询类方法演示
@@ -19,53 +20,53 @@ public function find($id){
1920
dd($resultObject);
2021
}
2122

22-
public function where(){
23+
public function whereGet(){
2324
// 条件检索数据表(条件数组)
2425
$conditionArray = ["author" => "Rytia"];
2526
$resultObjectArray = Demo::where($conditionArray)->get();
2627
dd($resultObjectArray);
2728
}
2829

29-
public function whereRaw(){
30+
public function whereRawGet(){
3031
// 条件检索数据表(SQL语句)
3132
$conditionStatement = 'author LIKE "Rytia"';
3233
$resultObjectArray = Demo::whereRaw($conditionStatement)->get();
3334
dd($resultObjectArray);
3435
}
3536

36-
public function all(){
37+
public function allGet(){
3738
// 显示全部数据
3839
$resultObjectArray = Demo::all();
3940
dd($resultObjectArray);
4041
}
4142

42-
public function raw(){
43+
public function rawGet(){
4344
// 执行SQL语句
4445
$sqlStatement = 'SELECT * FROM {table} WHERE author LIKE "Rytia"';
4546
$resultObjectArray = Demo::raw($sqlStatement)->get();
4647
dd($resultObjectArray);
4748
}
4849

49-
public function search(){
50+
public function searchGet(){
5051
// 数据表字段搜索
5152
$resultObjectArray = Demo::search("title", "%嗯%");
5253
dd($resultObjectArray);
5354
}
5455

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

62-
public function delete($id){
63+
public function deleteGet($id){
6364
// 删除条目
6465
$result = Demo::find($id)->delete();
6566
dd($result);
6667
}
6768

68-
public function paginate($pageNum){
69+
public function paginateGet($pageNum){
6970
// 条目分页演示
7071

7172
// 直接调用:相当于 Database 层分页,效率高
@@ -82,7 +83,7 @@ public function paginate($pageNum){
8283
dd($test);
8384
}
8485

85-
public function databaseWhere(){
86+
public function databaseWhereGet(){
8687
// 数据库层封装演示1
8788
$result = Database::table('demo')
8889
->setModel(Demo::class)
@@ -95,7 +96,7 @@ public function databaseWhere(){
9596

9697
}
9798

98-
public function databaseOn(){
99+
public function databaseOnGet(){
99100
// 数据库层封装演示2
100101
$result = Database::table('wiki')
101102
->select('DISTINCT zhong.name,wiki.coordinate')

App/System/Controller.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,26 @@
88
class Controller
99
{
1010
protected $request;
11+
protected $response;
1112

12-
public function response($data = null, $statusCode = 200){
13-
return $data;
13+
public function response($data = null, $statusCode = 200, $errcode = 0, $errmsg = ''){
14+
if(is_null($data)){
15+
return $this->response;
16+
} else {
17+
return $this->response->dataEncode($data, $statusCode, $errcode, $errmsg);
18+
}
19+
}
20+
21+
public function request($field){
22+
if(is_null($field)){
23+
return $this->request;
24+
} else {
25+
return $this->request->get($field);
26+
}
27+
}
28+
29+
public function __construct($request, $response){
30+
$this->request = $request;
31+
$this->response = $response;
1432
}
1533
}

App/System/Http/Request.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<?php
22
namespace System\Http;
3+
/**
4+
* PHP-QuickORM 框架 HTTP 请求类
5+
* @author Rytia <rytia@outlook.com>
6+
* @copyright 2018 PHP-QuickORM
7+
*/
38
Class Request
49
{
510
protected $method;
11+
protected $url;
12+
protected $path;
613
protected $postArray;
714
protected $getArray;
815

@@ -11,8 +18,10 @@
1118
* Request constructor.
1219
*/
1320
public function __construct(){
14-
// 获取请求的方式
21+
// 获取请求的信息
1522
$this->method = strtolower($_SERVER['REQUEST_METHOD']);
23+
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
24+
$this->url = $_SERVER["REQUEST_URI"];
1625
$this->postArray = $_POST;
1726
$this->getArray = $_GET;
1827
}
@@ -56,23 +65,23 @@ public function get($field){
5665
* 获取请求的路径
5766
* @return string
5867
*/
59-
public function path(){
60-
return dirname($_SERVER["REQUEST_URI"]);
68+
public function getPath(){
69+
return $this->path;
6170
}
6271

6372
/**
6473
* 获取请求的具体链接
6574
* @return string
6675
*/
67-
public function url(){
68-
return $_SERVER["REQUEST_URI"];
76+
public function getUrl(){
77+
return $this->url;
6978
}
7079

7180
/**
7281
* 获取请求的方法
7382
* @return string
7483
*/
75-
public function method(){
84+
public function getMethod(){
7685
return $this->method;
7786
}
7887

App/System/Http/Response.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,53 @@
11
<?php
2+
namespace System\Http;
3+
use System\Interfaces\Jsonable;
24
/**
3-
* Created by PhpStorm.
4-
* User: rytia
5-
* Date: 2018/8/20
6-
* Time: 14:13
5+
* PHP-QuickORM 框架 HTTP 响应类
6+
* @author Rytia <rytia@outlook.com>
7+
* @copyright 2018 PHP-QuickORM
78
*/
8-
// TODO: RESPONSE CLASS
9+
Class Response
10+
{
11+
private $httpVersion = "HTTP/1.1";
12+
13+
protected $header;
14+
protected $location;
15+
16+
protected $data;
17+
protected $statusCode;
18+
protected $errcode;
19+
protected $errmsg;
20+
21+
public function __construct(){
22+
// 如果需要定制默认 header,可以在此操作
23+
$header[] = "X-Powered-By: PHP-QuickORM";
24+
}
25+
26+
public function json($data, $statusCode = 200){
27+
if($data instanceof Jsonable){
28+
echo $data;
29+
} else {
30+
echo json_encode($data);
31+
}
32+
}
33+
34+
public function redirect($url = null){
35+
36+
}
37+
38+
public function back(){
39+
40+
}
41+
42+
public function dataEncode($data, $statusCode, $errcode = 0, $errmsg = ''){
43+
44+
}
45+
46+
public function setHeader(){
47+
48+
}
49+
50+
public function getHeader(){
51+
52+
}
53+
}

App/System/Http/Route.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
namespace System\Http;
3+
use ReflectionClass;
4+
use ReflectionException;
5+
/**
6+
* PHP-QuickORM 框架的路由系统
7+
* @author Rytia <rytia@outlook.com>
8+
* @copyright 2018 PHP-QuickORM
9+
*/
10+
Class Route
11+
{
12+
/**
13+
* 路由系统初始化
14+
* @param $request
15+
* @param $response
16+
* @param $autoRewrite
17+
*/
18+
public static function initialize(Request $request, Response $response, $autoRewrite = false){
19+
20+
// 划分请求
21+
$requestArray = self::getRequestArray($request, $autoRewrite);
22+
$controllerName = array_shift($requestArray)."Controller";
23+
$controllerMethod = array_shift($requestArray).ucfirst($request->getMethod());
24+
// 通过反射调用相应控制器
25+
try {
26+
$controller = new ReflectionClass('Controller\\'.$controllerName);
27+
$controller->newInstance($request, $response)->$controllerMethod(...$requestArray);
28+
} catch (ReflectionException $exception){
29+
trigger_error("未找到相应页面", E_USER_ERROR);
30+
}
31+
32+
}
33+
34+
/**
35+
* 把请求 URL 划分为数组
36+
* @param Request $request
37+
* @param bool $autoRewrite
38+
* @return array
39+
*/
40+
public static function getRequestArray(Request $request, $autoRewrite = false){
41+
if($autoRewrite){
42+
// 划分请求链接
43+
$requestUrl = $request->getPath();
44+
$requestArray = explode("/",$requestUrl);
45+
// 去除第一个元素
46+
array_shift($requestArray);
47+
return $requestArray;
48+
} else {
49+
// 划分请求链接
50+
$requestUrl = $_SERVER['QUERY_STRING'];
51+
return explode("/",$requestUrl);
52+
}
53+
}
54+
}

App/System/Interfaces/Jsonable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ interface Jsonable
99
* @uses 用于标记 __toString() 方法将实例格式化为 JSON 的类
1010
*/
1111
public function toJson($option = 0);
12+
13+
public function __toString();
1214
}

Public/index.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
<?php
22
require __DIR__.'/../vendor/autoload.php';
3+
use \System\Http\Route;
4+
use \System\Http\Request;
5+
use \System\Http\Response;
36

47
/**
58
* PHP-QuickORM
69
*
7-
* @description: a simple PHP ORM framework to built up api server
10+
* @description: a simple PHP ORM framework to built up an api server
811
* @author: Rytia
912
*/
1013

11-
set_error_handler('errorHandler');
14+
// 此为测试环境启动器,无需伪静态规则即可运行
15+
// 如果您已将产品真正投入使用,请将您的工作目录设置为 /Public,方便与静态文件的统一管理
1216

13-
$query = explode("/",$_SERVER['QUERY_STRING']);
14-
call_user_func(['Controller\\'.array_shift($query),array_shift($query)],...$query);
17+
// 启动相应函数
18+
require_once __DIR__ . "/App/System/Utility/Functions.php";
19+
20+
// 设置错误与异常处理
21+
set_error_handler('\System\Utility\Exception::errorReport');
22+
set_exception_handler('\System\Utility\Exception::exceptionReport');
23+
24+
// 启动路由系统处理请求
25+
Route::initialize(new Request(), new Response(), false);

index.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
<?php
22
require __DIR__.'/vendor/autoload.php';
3+
use \System\Http\Route;
4+
use \System\Http\Request;
5+
use \System\Http\Response;
36

47
/**
58
* PHP-QuickORM
69
*
7-
* @description: a simple PHP ORM framework to built up api server
10+
* @description: a simple PHP ORM framework to built up an api server
811
* @author: Rytia
912
*/
1013

1114
// 此为测试环境启动器,无需伪静态规则即可运行
1215
// 如果您已将产品真正投入使用,请将您的工作目录设置为 /Public
1316

14-
17+
// 启动相应函数
1518
require_once __DIR__ . "/App/System/Utility/Functions.php";
19+
20+
// 设置错误与异常处理
1621
set_error_handler('\System\Utility\Exception::errorReport');
1722
set_exception_handler('\System\Utility\Exception::exceptionReport');
1823

19-
$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
20-
21-
$query = explode("/",$request);
22-
array_shift($query); // 去除数组第一个空元素
23-
24-
// TODO: 重新设计路由系统,Controller 需要实例化
25-
$controller = new ReflectionClass('Controller\\'.array_shift($query));
26-
$function = array_shift($query);
27-
$controller->newInstance()->$function(...$query);
24+
// 启动路由系统处理请求
25+
Route::initialize(new Request(), new Response(), true);

vendor/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
'System\\DatabaseDriver\\pdo_mysql' => $baseDir . '/App/System/DatabaseDriver/pdo_mysql.php',
1313
'System\\DatabaseDriver\\pdo_sqlite' => $baseDir . '/App/System/DatabaseDriver/pdo_sqlite.php',
1414
'System\\Http\\Request' => $baseDir . '/App/System/Http/Request.php',
15+
'System\\Http\\Response' => $baseDir . '/App/System/Http/Response.php',
16+
'System\\Http\\Route' => $baseDir . '/App/System/Http/Route.php',
1517
'System\\Interfaces\\Jsonable' => $baseDir . '/App/System/Interfaces/Jsonable.php',
1618
'System\\Model' => $baseDir . '/App/System/Model.php',
1719
'System\\Utility\\Exception' => $baseDir . '/App/System/Utility/Exception.php',

0 commit comments

Comments
 (0)