feat: Add logic for redirect

This commit is contained in:
overflowerror 2023-11-29 22:40:15 +01:00
parent f5d5166b4b
commit e7156641fc
6 changed files with 28 additions and 6 deletions

View file

@ -2,3 +2,5 @@
const DB_CONNECTION = "DB_CONNECTION"; const DB_CONNECTION = "DB_CONNECTION";
const REPOSITORIES = "REPOSITORIES"; const REPOSITORIES = "REPOSITORIES";
const REQUEST_PATH = "REQUEST_PATH";

View file

@ -1,5 +1,14 @@
<?php <?php
require_once(ROOT . "/utils/error.php");
return function (array &$context) { return function (array &$context) {
echo "Hello World 2"; $path = substr($context[REQUEST_PATH], 1);
$url = $context[REPOSITORIES]->urls->getBySlug($path);
if (!$url) {
redirect("/");
} else {
redirect($url->url);
}
}; };

View file

@ -4,15 +4,15 @@ class URL {
public int $id; public int $id;
public DateTime $created; public DateTime $created;
public DateTime $updated; public DateTime $updated;
public DateTime $deleted; public ?DateTime $deleted;
public string $slug; public string $slug;
public string $url; public string $url;
public string $accessKey; public ?string $accessKey;
public function __construct( public function __construct(
string $slug, string $slug,
string $url, string $url,
string $accessKey ?string $accessKey
) { ) {
$this->slug = $slug; $this->slug = $slug;
$this->url = $url; $this->url = $url;

View file

@ -34,7 +34,14 @@ class URLs {
if ($statement->rowCount() == 0) { if ($statement->rowCount() == 0) {
return null; return null;
} else { } else {
return $statement->fetch(); $row = $statement->fetch();
$url = new URL($row["slug"], $row["url"], $row["access_key"]);
$url->id = $row["id"];
$url->created = new DateTime($row["created"]);
$url->updated = new DateTime($row["updated"]);
$url->deleted = ($row["deleted"]) ? new DateTime($row["deleted"]) : null;
return $url;
} }
} }
} }

View file

@ -64,7 +64,7 @@ class Router {
$route = $this->notFoundHandler; $route = $this->notFoundHandler;
} }
$context["REQUEST_PATH"] = $path; $context[REQUEST_PATH] = $path;
return $route($context); return $route($context);
} }

View file

@ -1,5 +1,9 @@
<?php <?php
function redirect(string $url) {
header("Location: " . $url, true, 302);
}
function setStatusCode(int $status) { function setStatusCode(int $status) {
header("HTTP/", true, $status); header("HTTP/", true, $status);
} }