mirror of
https://github.com/sigmasternchen/drnk.me
synced 2025-03-15 09:48:54 +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 DB_CONNECTION = "DB_CONNECTION";
|
||||||
const REPOSITORIES = "REPOSITORIES";
|
const REPOSITORIES = "REPOSITORIES";
|
||||||
|
|
||||||
|
const REQUEST_PATH = "REQUEST_PATH";
|
|
@ -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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue