commit c974ddfe591fb255d8c08b4d61471977da32aa8c
parent 4408df8030a54fb1a614b77200779042bca07851
Author: Friedel Schön <[email protected]>
Date: Sun, 25 Jun 2023 17:29:40 +0200
mime type router by extension final
Diffstat:
1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/Lollipop/Router.php b/Lollipop/Router.php
@@ -1,11 +1,11 @@
-<?php
+<?php
-const MIME_TYPES = array(
- "css" => "text/css",
- "js" => "text/javascript"
-);
-
namespace Lollipop {
+ const MIME_TYPES = array(
+ "css" => "text/css",
+ "js" => "text/javascript"
+ );
+
class Router
{
protected array $routes = [];
@@ -16,13 +16,18 @@ namespace Lollipop {
$this->temp = $temp;
}
- protected function set_mime(string $file) {
- $ext = pathinfo($file, PATHINFO_EXTENSION);
+ protected function set_mime($file) {
+ if (!is_null($file))
+ $ext = pathinfo($file, PATHINFO_EXTENSION);
+ else
+ $ext = null;
- if (array_key_exists($ext, MIME_TYPES))
- return MIME_TYPES[$ext];
+ if ($ext != null && array_key_exists($ext, MIME_TYPES))
+ $mime = MIME_TYPES[$ext];
else
- return "text/html";
+ $mime = "text/html";
+
+ header("Content-Type: $mime");
}
protected function match(string $match, array &$route_vars): bool
@@ -84,12 +89,12 @@ namespace Lollipop {
if (is_callable($route["func"])) {
$fil = $route["func"]($vars);
if (!is_null($fil))
- echo $this->temp->template($fil, $vars);
$this->set_mime($fil);
+ echo $this->temp->template($fil, $vars);
return;
} else {
+ $this->set_mime($route["func"]);
echo $this->temp->template($route["func"], $vars);
- $this->set_mime($fil);
return;
}
}