Skip to content

Commit 483c59c

Browse files
committed
work!
1 parent 3ab3048 commit 483c59c

File tree

6 files changed

+31
-52
lines changed

6 files changed

+31
-52
lines changed

config/Database.php renamed to app/Config/Database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\config;
3+
namespace App\Config;
44

55
use PDO;
66

@@ -13,6 +13,7 @@ class Database
1313
*
1414
* @return \PDO
1515
*/
16+
1617
public static function connect()
1718
{
1819
if (is_null(self::$dbh)) {

app/Controllers/UserController.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44

55
use App\Http\Request;
66
use App\Http\Response;
7-
use App\Models\Users;
87

98
class UserController extends Controller
109
{
10+
1111
/**
1212
* Display a listing of the resource.
1313
*
1414
* @return \Http\Request
1515
*/
1616
public function index(Request $request)
1717
{
18-
$users = Users::all();
19-
Response::json($users);
18+
//
2019
}
2120

2221
/**
@@ -26,7 +25,7 @@ public function index(Request $request)
2625
*/
2726
public function store(Request $request)
2827
{
29-
Users::create($request->json());
28+
//
3029
}
3130

3231
/**
@@ -36,8 +35,7 @@ public function store(Request $request)
3635
*/
3736
public function show(Request $request)
3837
{
39-
$user = Users::find($request->params->id);
40-
Response::json($user);
38+
//
4139
}
4240

4341
/**
@@ -47,7 +45,7 @@ public function show(Request $request)
4745
*/
4846
public function update(Request $request)
4947
{
50-
Users::update($request->input(), $request->params->id);
48+
//
5149
}
5250

5351
/**
@@ -57,6 +55,6 @@ public function update(Request $request)
5755
*/
5856
public function destroy(Request $request)
5957
{
60-
Users::delete($request->params->id);
58+
//
6159
}
6260
}

app/Http/Request.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,30 @@ class Request
1212

1313
public function __construct($args)
1414
{
15+
$this->method = $args->method;
1516
$this->params = $args->params;
1617
$this->query = $args->query;
18+
$this->url = $args->url;
19+
$this->path = $args->path;
1720
$this->contentType = $_SERVER["CONTENT_TYPE"] ?? null;
18-
$this->method = $_SERVER["REQUEST_METHOD"] ?? null;
19-
$this->path = $_SERVER["REQUEST_URI"] ?? null;
2021
$this->authorization = $_SERVER["HTTP_AUTHORIZATION"] ?? null;
2122
}
2223

2324
public function json()
2425
{
2526
if ($this->method !== "POST" || $this->contentType !== "application/json") {
26-
return [];
27+
return null;
2728
}
28-
$json = json_decode(trim(file_get_contents("php://input")));
2929

30-
return json_last_error() === JSON_ERROR_NONE ? $json : [];
30+
$value = json_decode(file_get_contents("php://input"));
31+
32+
return json_last_error() === JSON_ERROR_NONE ? $value : null;
3133
}
3234

33-
public function input()
35+
public function form()
3436
{
35-
if ($this->method === "PUT") {
36-
parse_str(file_get_contents("php://input"), $body);
37-
return (object)$body;
38-
}
39-
4037
if ($this->method !== "POST") {
41-
return [];
38+
return null;
4239
}
4340

4441
$body = [];

app/Routing/Route.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class Route extends RouteCollector
2424
protected $currentRoute = null;
2525

2626
/**
27-
* The query names for the route.
28-
*
29-
* @var string[]
27+
* The args names for the route.
28+
*
29+
* @var object
3030
*/
31-
private $query = [];
31+
protected $args;
3232

3333
/**
3434
* All of the verbs supported by the router.
@@ -39,18 +39,15 @@ class Route extends RouteCollector
3939

4040
public function __construct()
4141
{
42-
$this->req = $this->request();
43-
$this->method = $this->req->method;
44-
$this->path = $this->req->path;
45-
$this->query = $this->req->query;
42+
$this->args = $this->request_url();
4643
}
4744

4845
public function call()
4946
{
50-
$routes = $this->filter_routes_by_method(self::$routes, $this->method);
47+
$routes = $this->filter_routes_by_method(self::$routes, $this->args->method);
5148

5249
foreach ($routes as $value) {
53-
if ($this->matches($value["uri"], $this->path)) {
50+
if ($this->matches($value["uri"], $this->args->path)) {
5451
$this->currentRoute = (object) $value;
5552
break 1;
5653
}
@@ -59,13 +56,8 @@ public function call()
5956
if (!$this->currentRoute) {
6057
return Response::json(HttpException::HttpNotFoundException());
6158
}
62-
63-
$args = (object)[
64-
"params" => $this->params,
65-
"query" => (object) $this->query
66-
];
67-
68-
return call_user_func($this->currentRoute->callback, (new Request($args)));
59+
$this->args->params = $this->params;
60+
return call_user_func($this->currentRoute->callback, (new Request($this->args)));
6961
}
7062

7163
/**
@@ -106,7 +98,7 @@ private function matches(string $uri, string $path)
10698
return false;
10799
}
108100

109-
private function request()
101+
private function request_url()
110102
{
111103
$httpMethod = $_SERVER["REQUEST_METHOD"] ?? null;
112104

@@ -120,12 +112,12 @@ private function request()
120112
return Response::json(HttpException::HttpBadRequestException());
121113
}
122114

123-
parse_str($_SERVER["QUERY_STRING"], $qs);
115+
parse_str($_SERVER["QUERY_STRING"] ?? "", $qs);
124116

125-
return ((object) array_merge(parse_url($URL), [
117+
return (object) array_merge(parse_url($URL), [
118+
"url" => $URL,
126119
"method" => $httpMethod,
127120
"query" => (object)$qs,
128-
"contentType" => ($_SERVER["CONTENT_TYPE"]) ?? null
129-
]));
121+
]);
130122
}
131123
}

app/Routing/RouteCollector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Routing;
44

5-
65
class RouteCollector
76
{
87

public/index.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use App\Application;
44
use App\Routing\Route;
55
use App\Controllers\UserController;
6-
use App\Http\Request;
7-
use App\Http\Response;
86

97
require __DIR__ . '/../vendor/autoload.php';
108

@@ -28,10 +26,4 @@
2826
Route::put('/users/{id}', [UserController::class, 'update']);
2927
Route::delete('/users/{id}', [UserController::class, 'destroy']);
3028

31-
Route::get('/{username}', function (Request $request) {
32-
Response::json($request);
33-
echo "👋👋 " . $request->params->username . " i 🧡 " . $request->query->city;
34-
});
35-
36-
3729
$app->run();

0 commit comments

Comments
 (0)