mirror of
https://github.com/sigmasternchen/drnk.me
synced 2025-03-15 01:38:55 +00:00
feat: Add logic for redirect
This commit is contained in:
parent
f5d5166b4b
commit
e7156641fc
6 changed files with 28 additions and 6 deletions
|
@ -2,3 +2,5 @@
|
|||
|
||||
const DB_CONNECTION = "DB_CONNECTION";
|
||||
const REPOSITORIES = "REPOSITORIES";
|
||||
|
||||
const REQUEST_PATH = "REQUEST_PATH";
|
|
@ -1,5 +1,14 @@
|
|||
<?php
|
||||
|
||||
require_once(ROOT . "/utils/error.php");
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -4,15 +4,15 @@ class URL {
|
|||
public int $id;
|
||||
public DateTime $created;
|
||||
public DateTime $updated;
|
||||
public DateTime $deleted;
|
||||
public ?DateTime $deleted;
|
||||
public string $slug;
|
||||
public string $url;
|
||||
public string $accessKey;
|
||||
public ?string $accessKey;
|
||||
|
||||
public function __construct(
|
||||
string $slug,
|
||||
string $url,
|
||||
string $accessKey
|
||||
?string $accessKey
|
||||
) {
|
||||
$this->slug = $slug;
|
||||
$this->url = $url;
|
||||
|
|
|
@ -34,7 +34,14 @@ class URLs {
|
|||
if ($statement->rowCount() == 0) {
|
||||
return null;
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ class Router {
|
|||
$route = $this->notFoundHandler;
|
||||
}
|
||||
|
||||
$context["REQUEST_PATH"] = $path;
|
||||
$context[REQUEST_PATH] = $path;
|
||||
|
||||
return $route($context);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
<?php
|
||||
|
||||
function redirect(string $url) {
|
||||
header("Location: " . $url, true, 302);
|
||||
}
|
||||
|
||||
function setStatusCode(int $status) {
|
||||
header("HTTP/", true, $status);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue