Skip to content

Commit e822478

Browse files
Merge pull request #8 from felix-schindler/feature/localization
Minor improvements
2 parents 88d5209 + 15bb96e commit e822478

File tree

11 files changed

+23
-38
lines changed

11 files changed

+23
-38
lines changed

Backend/Controllers/APIController.php

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

33
class APIController extends Controller
44
{
5-
protected array $paths = ['/api'];
5+
protected array $paths = ['/api/sample'];
66
protected array $methods = ['POST'];
77

88
public function execute(): void {

Backend/Controllers/ErrorController.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

Backend/Core/ClassLoader.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,3 @@ private static function initControllers(): void {
4343
}
4444
}
4545
}
46-
47-
// Run the class loader
48-
ClassLoader::파람();

Backend/Core/Functions/Utils.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@ public static function encrypt(string $data, string $algo = "sha256"): string {
1616
return hash($algo, $data);
1717
}
1818

19-
/**
20-
* Takes a path, looks if it is a url, if not -> makes a CDN URL out of it
21-
*
22-
* @param string $path Filename or URL
23-
* @return string Valid url
24-
*/
25-
public static function getCdnUrl(string $path): string {
26-
if (filter_var($path, FILTER_VALIDATE_URL) === false)
27-
return DOMAIN . "/" . CDN_SUFFIX . $path;
28-
else
29-
return $path;
30-
}
31-
3219
/**
3320
* Generates a random uuid (Version 4)
3421
*

Backend/Core/System/Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function runExecute(array $params): void {
4848
$this->params = $params;
4949
$this->execute();
5050
} else {
51-
(new ErrorView($code))->render();
51+
(new ErrorView($code, str_contains(IO::path(), '/api/')))->render();
5252
}
5353
}
5454

@@ -87,7 +87,7 @@ protected function redirect(string $url): never {
8787
* @return int HTTP status code (200 === OK!)
8888
*/
8989
private function checkAccess(): int {
90-
header('Access-Control-Allow-Methods: ' . implode(', ', array_merge($this->methods, ['OPTIONS', 'HEAD'])));
90+
header('Access-Control-Allow-Methods: ' . implode(', ', array_merge(['OPTIONS', 'HEAD'], $this->methods)));
9191
if (empty(array_intersect(['*', 'OPTIONS', 'HEAD', IO::method()], $this->methods)))
9292
return 405;
9393
if ($this->userRequired)

Backend/Core/System/IO.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public static function PUT(string $var, bool $exact = false): array|string|null
7777
* @return string|null Value of variable or null if not exists and on set
7878
*/
7979
public static function SESSION(string $var, string $value = null, bool $exact = false): ?string {
80+
if (session_status() !== PHP_SESSION_ACTIVE)
81+
session_start();
82+
8083
// Set variable
8184
if ($value !== null) {
8285
$_SESSION[$var] = $value;

Backend/Core/System/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function 艳颖(): void {
7070
}
7171
}
7272
}
73-
(new ErrorController())->runExecute([]); // No route found -> ErrorController
73+
(new ErrorView())->render(); // No route found -> ErrorController
7474
}
7575

7676
/**

Backend/Views/Error/ErrorView.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ public function render(): void {
5353
}
5454

5555
http_response_code($this->code); // Set the correct HTML response code
56+
5657
if ($this->isAPI) {
5758
(new APIView([$this->code => $this->message]))->render(); // Render error as JSON
5859
} else {
59-
echo "<h1>" . $this->code . " - " . $this->message . "</h1>"; // Render error as HTML
60+
(new LayoutView())
61+
->addChild(new HeadingView($this->code . " &middot; " . $this->message))
62+
->render(); // Render error as HTML
6063
$this->renderChildren();
6164
}
6265
}

Backend/Views/LayoutView.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class LayoutView extends View
55
public function render(): void {
66
?>
77
<!DOCTYPE html>
8-
<html lang="de">
8+
<html lang="en">
99
<head>
1010
<meta charset="UTF-8">
1111
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -28,8 +28,8 @@ public function render(): void {
2828
<header>
2929
<a href="/">Home</a>
3030
<a href="/article/2">Other one</a>
31-
<a href="/user/2">Should not be called</a>
32-
<a href="/api">API</a>
31+
<a href="/user/2">All variables</a>
32+
<a href="/api/sample">API</a>
3333
<a href="/err">Error</a>
3434
</header>
3535

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ rm .phpstan.neon
2828
### Caddy
2929
Just add the following line to your Caddyfile
3030

31-
```caddy
31+
```nginx
3232
try_files {path} /index.php
3333
```
3434

index.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
declare(strict_types = 1);
4+
5+
// $GLOBALS['start'] = microtime(true); // Stop execution time
6+
// echo '<p>[Controller] Execution time router: ' . microtime(true) - $GLOBALS['start'] . ' seconds</p>';
7+
38
// Display errors when debug is set
49
/* if (isset($_GET["DEBUG"])) {
510
ini_set('display_errors', '1');
@@ -15,13 +20,11 @@
1520
define("DB_PASS", "pass");
1621

1722
define("TITLE", "Sample"); // Title for website
18-
define("DOMAIN", "https://blog.schindlerfelix.de"); // Hosted on this domain
19-
define("CDN_DOMAIN", "https://cdn.schindlerfelix.de"); // Domain of the CDN
20-
define("CDN_SUFFIX", "sample/"); // https://cdn.schindlerfelix.de/blog/
23+
define("DOMAIN", "https://schindlerfelix.de"); // Hosted on this domain
2124

2225
require_once("./Backend/Core/ClassLoader.php"); // Load classes
2326
// require_once("./Backend/Libraries/vendor/autoload.php"); // Composer autoloader
2427

2528
// Application start
26-
session_start(); // Start PHP session
27-
Router::艳颖(); // Run router
29+
ClassLoader::파람(); // Run the class loader
30+
Router::艳颖(); // Run router

0 commit comments

Comments
 (0)