Skip to content

Commit 3623896

Browse files
committed
update view and add refactor get&post request function
1 parent bfe19f0 commit 3623896

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($rootPath, array $config)
3838
$primaryValue = $this->session->get('user');
3939
if ($primaryValue) {
4040
$primaryKey = $this->userClass::primaryKey();
41-
$this->user = $this->userClass::FindOne([$primaryKey => $primaryValue]);
41+
$this->user = $this->userClass::get([$primaryKey => $primaryValue]);
4242
} else {
4343
$this->user = null;
4444
}

Request.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,23 @@ public function isPost()
3232
return $this->method() === 'post';
3333
}
3434

35-
public function getBody()
35+
public function post()
3636
{
37-
$body = [];
38-
39-
if ($this->method() === 'get') {
40-
foreach ($_GET as $key => $value) {
41-
$body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
42-
}
37+
$data = [];
38+
foreach ($_POST as $key => $value) {
39+
$data[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
4340
}
4441

45-
if ($this->method() === 'post') {
46-
foreach ($_POST as $key => $value) {
47-
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
48-
}
42+
return $data;
43+
}
44+
45+
public function get()
46+
{
47+
$data = [];
48+
foreach ($_GET as $key => $value) {
49+
$data[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
4950
}
5051

51-
return $body;
52+
return $data;
5253
}
5354
}

Router.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function resolve()
3737
}
3838

3939
if (is_string($callback)) {
40-
return Application::$app->view->renderView($callback);
40+
return $this->renderView($callback);
4141
}
4242

4343
if (is_array($callback)) {
@@ -53,4 +53,14 @@ public function resolve()
5353

5454
return call_user_func($callback, $this->request, $this->response);
5555
}
56+
57+
public function renderView($view, $params = [])
58+
{
59+
return Application::$app->view->renderView($view, $params);
60+
}
61+
62+
public function renderViewOnly($view, $params = [])
63+
{
64+
return Application::$app->view->renderViewOnly($view, $params);
65+
}
5666
}

db/DbModel.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,26 @@ public function save()
1717
$tableName = $this->tableName();
1818
$attributes = $this->attributes();
1919
$params = array_map(fn($attr) => ":$attr", $attributes);
20-
$statement = self::prepare("INSERT INTO $tableName (".implode(',', $attributes).")
21-
VALUES(".implode(',', $params).")"
22-
);
2320

24-
foreach ($attributes as $attribute) {
25-
$statement->bindValue(":$attribute", $this->{$attribute});
21+
if ($this->id) {
22+
$query_string = '';
23+
foreach ($attributes as $attribute ) {
24+
$query_string .= "$attribute=:$attribute,";
25+
}
26+
$query_string = rtrim($query_string, ',');
27+
$statement = self::prepare("UPDATE $tableName SET $query_string WHERE id=$this->id;");
28+
29+
foreach ($attributes as $attribute) {
30+
$statement->bindValue(":$attribute", $this->{$attribute});
31+
}
32+
} else {
33+
$statement = self::prepare("INSERT INTO $tableName (".implode(',', $attributes).")
34+
VALUES(".implode(',', $params).")"
35+
);
36+
37+
foreach ($attributes as $attribute) {
38+
$statement->bindValue(":$attribute", $this->{$attribute});
39+
}
2640
}
2741

2842
$statement->execute();
@@ -34,7 +48,7 @@ public function prepare($sql)
3448
return Application::$app->db->pdo->prepare($sql);
3549
}
3650

37-
public function findOne($where)
51+
public function get($where)
3852
{
3953
$tableName = static::tableName();
4054
$attributes = array_keys($where);

0 commit comments

Comments
 (0)