Skip to content
This repository was archived by the owner on Jun 18, 2023. It is now read-only.

Commit 8955f43

Browse files
committed
fix page 404
1 parent ef09cb6 commit 8955f43

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

app/Controllers/PageController.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Controllers;
44

55
use App\DI;
6+
use App\Models\Page;
7+
use Dewep\Exception\HttpException;
68
use Dewep\Http\Request;
79

810
/**
@@ -25,7 +27,8 @@ public function index(Request $request): string
2527
return DI::twig()->render(
2628
'page.html.twig',
2729
[
28-
'name' => 'home',
30+
'title' => 'Home',
31+
'text' => 'This is - Home',
2932
]
3033
);
3134
}
@@ -35,16 +38,31 @@ public function index(Request $request): string
3538
* @param string $id
3639
*
3740
* @return string
41+
* @throws HttpException
3842
* @throws \Twig\Error\LoaderError
3943
* @throws \Twig\Error\RuntimeError
4044
* @throws \Twig\Error\SyntaxError
4145
*/
4246
public function page(Request $request, string $id): string
4347
{
48+
$query = <<<'SQL'
49+
SELECT * FROM `page` WHERE `id`=:id;
50+
SQL;
51+
52+
/** @var Page|false $page */
53+
$page = DI::db()->select($query, [':id' => (int)$id])
54+
->asClass(Page::class)
55+
->getOne();
56+
57+
if (!$page instanceof Page) {
58+
throw new HttpException('Page not found', 404);
59+
}
60+
4461
return DI::twig()->render(
4562
'page.html.twig',
4663
[
47-
'name' => $id,
64+
'title' => $page->getTitle(),
65+
'text' => $page->getText(),
4866
]
4967
);
5068
}

app/HttpCodes.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App;
44

5+
use Dewep\Http\Response;
6+
57
/**
68
* Class HttpCodes
79
*
@@ -10,13 +12,20 @@
1012
class HttpCodes
1113
{
1214
/**
15+
* @param array $error
16+
*
1317
* @return string
1418
* @throws \Twig\Error\LoaderError
1519
* @throws \Twig\Error\RuntimeError
1620
* @throws \Twig\Error\SyntaxError
1721
*/
18-
public static function httpCode404(): string
22+
public static function httpCode404(array $error): string
1923
{
20-
return DI::twig()->render('404.html.twig', []);
24+
return DI::twig()->render(
25+
'404.html.twig',
26+
[
27+
'text' => $error['errorMessage'] ?? 'Error 404',
28+
]
29+
);
2130
}
2231
}

resources/404.html.twig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
Page not found
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>404</title>
5+
</head>
6+
<body>
7+
{{ text }}
8+
</body>
9+
</html>

resources/page.html.twig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
{{ name }}
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>{{ title }}</title>
5+
</head>
6+
<body>
7+
{{ text }}
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)