From bf5c678f6677af25a53420f1e478638ba8e82bfb Mon Sep 17 00:00:00 2001 From: sorenpeter Date: Sun, 8 Oct 2023 20:31:49 +0200 Subject: [PATCH] Routing implemented --- _wip_todo/views/home.php | 53 -------------------- index.php | 69 +++++++++++++------------- index_old.php | 38 ++++++++++++++ libs/Slimdown.php | 2 +- libs/twtxt.php | 2 +- partials/header.php | 2 +- partials/timeline.php | 11 ++-- _wip_todo/router.php => router.php | 0 follow.php => views/add_feed.php | 0 views/conv.php | 41 +++++++++++++++ following.php => views/following.php | 0 {_wip_todo/views => views}/gallery.php | 0 views/home.php | 40 +++++++++++++++ login.php => views/login.php | 0 new_twt.php => views/new_twt.php | 0 views/post.php | 34 +++++++++++++ {_wip_todo/views => views}/profile.php | 0 17 files changed, 198 insertions(+), 94 deletions(-) delete mode 100644 _wip_todo/views/home.php create mode 100644 index_old.php rename _wip_todo/router.php => router.php (100%) rename follow.php => views/add_feed.php (100%) create mode 100644 views/conv.php rename following.php => views/following.php (100%) rename {_wip_todo/views => views}/gallery.php (100%) create mode 100644 views/home.php rename login.php => views/login.php (100%) rename new_twt.php => views/new_twt.php (100%) create mode 100644 views/post.php rename {_wip_todo/views => views}/profile.php (100%) diff --git a/_wip_todo/views/home.php b/_wip_todo/views/home.php deleted file mode 100644 index 1aa6520..0000000 --- a/_wip_todo/views/home.php +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - - <?=$title?> - Timeline - - - - - - - - - -
- -

- - Twts for - - Timeline for - -

- - - -
- - - - - diff --git a/index.php b/index.php index aaeae64..9311c79 100644 --- a/index.php +++ b/index.php @@ -2,45 +2,46 @@ //require_once("router.php"); //require_once("views/home.php"); -require_once("partials/base.php"); +//require_once("partials/base.php"); -?> +$request = $_SERVER['REQUEST_URI']; +$path = parse_url($request, PHP_URL_PATH); +// $query = parse_url($request, PHP_URL_QUERY); - - - - - - Timeline - - +$viewDir = '/views/'; - +// Define your routes using regular expressions -
- - 'home.php', + '/admin' => 'admin.php', + '/about' => 'about.php', + '/profile/([a-zA-Z0-9_-]+)' => 'profile.php', + '/conv/([a-zA-Z0-9]{7})' => 'conv.php', // matches only twtHash of exactly 7 alphanumeric characters + '/post/([a-zA-Z0-9]{7})' => 'post.php', // matches only twtHash of exactly 7 alphanumeric characters +]; - // TODO: Give a propper error if feed is not valid - if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) { - die('Not a valid URL'); +// Loop through the defined routes and try to match the request URI +foreach ($routes as $pattern => $action) { + if (preg_match('#^' . $pattern . '$#', $path, $matches)) { + + // Extract any matched parameters (e.g., username) + if(!empty($matches[1])) { + //array_shift($matches); + $id = $matches[1]; + } + + // Load the corresponding action (view) + require __DIR__ . $viewDir . $action; + exit; // Stop processing further routes } +} - // $parsedTwtxtFile = getTwtsFromTwtxtString($twtsURL); - if (!is_null($parsedTwtxtFile)) { - $parsedTwtxtFiles[$parsedTwtxtFile->mainURL] = $parsedTwtxtFile; - include 'partials/profile.php'; - } -} ?> +// If no matching route is found, handle as a 404 +http_response_code(404); +echo "

Oops! Page not found.

"; - - - -
- - - - - +/* Credit: + - PHP FOR BEGINNERS #4 - Create a dynamic Router: https://www.youtube.com/watch?v=eaHBK2XJ5Io + - https://chat.openai.com/c/3082a22a-d70e-4740-891c-9872f5da2180 +*/ \ No newline at end of file diff --git a/index_old.php b/index_old.php new file mode 100644 index 0000000..edf22a5 --- /dev/null +++ b/index_old.php @@ -0,0 +1,38 @@ + + + + + + Timeline + + + + + +
+ +mainURL] = $parsedTwtxtFile; + include 'partials/profile.php'; + } +} ?> + + + + +
+ + + + + \ No newline at end of file diff --git a/libs/Slimdown.php b/libs/Slimdown.php index 971387f..81e26dd 100644 --- a/libs/Slimdown.php +++ b/libs/Slimdown.php @@ -25,7 +25,7 @@ class Slimdown { public static $rules = array ( '/```(.*?)```/s' => self::class .'::code_parse', // code blocks //'/\n(#+)(.*)/' => self::class .'::header', // headers - '/\n(#\s+)(.*)/' => self::class .'::header', // headers - only with a space between # and text, to avoid matching with `#hashtags` + '/\n(#\s+)(.*)/' => self::class .'::header', // headers - only with a space between # and text, to avoid matching with `#hashtags` //'/\!\[([^\[]+)\]\(([^\)]+)\)/' => self::class .'::img', // images '/\!\[(.*?)\]\(([^\)]+)\)/' => self::class .'::img', // images 2 '/\[([^\[]+)\]\(([^\)]+)\)/' => self::class .'::link', // links diff --git a/libs/twtxt.php b/libs/twtxt.php index f9b680e..e857f8e 100644 --- a/libs/twtxt.php +++ b/libs/twtxt.php @@ -139,7 +139,7 @@ function replaceMentionsFromTwt(string $twtString): string { function replaceLinksFromTwt(string $twtString) { // Regular expression pattern to match URLs $pattern = '/(?$1'; $result = preg_replace($pattern, $replacement, $twtString); diff --git a/partials/header.php b/partials/header.php index bab8a96..027e7b2 100644 --- a/partials/header.php +++ b/partials/header.php @@ -1,7 +1,7 @@