diff --git a/classes/handlers/URI.php b/classes/handlers/URI.php index c97fb36..0c45f7b 100644 --- a/classes/handlers/URI.php +++ b/classes/handlers/URI.php @@ -44,61 +44,43 @@ public function __construct($app) { // Reuse the application $this->app = $app; - // Establishing up global context $GLOBALS["context"] = $app; - // Get the current URI without query parameters $request_uri = strtok($_SERVER['REQUEST_URI'], '?'); - // Tentative condition - // if ($request_uri == DIRECTORY_SEPARATOR && $request_uri !== "/index.php" && - // isset($_SERVER['QUERY_STRING']) && strpos($_SERVER['QUERY_STRING'], '?') !== false) { - - // if ($request_uri !== "/" && - // isset($_SERVER['QUERY_STRING']) && strpos($_SERVER['QUERY_STRING'], '?') !== false) { - // // Remove leading and trailing slashes, if any - // $request_uri = strtolower(trim($request_uri, DIRECTORY_SEPARATOR)); - // // echo var_dump($request_uri); - // // Define a base directory for your PHP files - // $controller_dir = $this->app->getControllersDir(); - // // Construct the full path to the PHP file based on the URI - // $file_path = $controller_dir . DIRECTORY_SEPARATOR . $request_uri . '.php'; - // // Check if the file exists - // if (file_exists($file_path)) { - // $this->app->messages[] = "File exists"; - // } else { - // // Page not found, return a 404 response - // // pageNotFound(); - // header("HTTP/1.0 404 Not Found"); - // echo "404 Page Not Found"; - // exit(0); - // } - // } else { - // } + // Define a base directory for your PHP files + $controller_dir = $this->app->getControllersDir(); - // Tentative move - // TODO: Move all of these to /controllers dir - if ($request_uri === "/") { - // $this->app->messages[] = "Home Page"; - include_once($this->app->getBaseDir() . "home.php"); - $this->processed = true; - } elseif ($request_uri === "/forgotpassword") { - include_once($this->app->getBaseDir() . "forgotpassword.php"); - $this->processed = true; - } elseif ($request_uri === "/register") { - include_once($this->app->getBaseDir() . "register.php"); - $this->processed = true; - } elseif ($request_uri === "/multiuser") { - $this->app->messages[] = "Home Page"; - include_once($this->app->getBaseDir() . "multiuser.php"); - $this->processed = true; - } elseif ($request_uri === "/logout") { - include_once($this->app->getBaseDir() . "logout.php"); - $this->processed = true; + // Tentative condition + // var_dump($request_uri); + if ($request_uri == DIRECTORY_SEPARATOR) { + $file_path = $controller_dir . 'home.php'; + // Check if the file exists + // var_dump($file_path); + if (file_exists($file_path)) { + // $this->app->messages[] = "File exists"; + require_once($file_path); + } else { + // Page not found, return a 404 response + $this->pageNotFound(); + } + } elseif ($request_uri !== DIRECTORY_SEPARATOR) { + // Remove leading and trailing slashes, if any + $request_uri = strtolower(trim($request_uri, DIRECTORY_SEPARATOR)); + // echo var_dump($request_uri); + // Construct the full path to the PHP file based on the URI + $file_path = $controller_dir . $request_uri . '.php'; + // Check if the file exists + // var_dump($file_path); + if (file_exists($file_path)) { + // $this->app->messages[] = "File exists"; + require_once($file_path); + } else { + // Page not found, return a 404 response + $this->pageNotFound(); + } } else { - header("HTTP/1.0 404 Not Found"); - echo "404 Page Not Found"; - exit(); + header("Location: " . DIRECTORY_SEPARATOR); } } @@ -111,4 +93,11 @@ public function isClassProcessed() { return $this->processed; } + + private function pageNotFound() + { + header("HTTP/1.0 404 Not Found"); + echo "404 Page Not Found"; + exit(0); + } } diff --git a/composer.json b/composer.json index 8fd510a..ba47b17 100644 --- a/composer.json +++ b/composer.json @@ -36,10 +36,10 @@ "composer run nodejs" ], "lint": [ - "./vendor/bin/phpcs classes/ libraries/ *.php" + "./vendor/bin/phpcs classes/ controllers/ libraries/" ], "lintfix": [ - "./vendor/bin/phpcbf classes/ libraries/ *.php" + "./vendor/bin/phpcbf classes/ controllers/ libraries/" ], "nodejs": [ "npm install" diff --git a/forgotpassword.php b/controllers/forgotpassword.php similarity index 95% rename from forgotpassword.php rename to controllers/forgotpassword.php index 69950ea..72fc38a 100644 --- a/forgotpassword.php +++ b/controllers/forgotpassword.php @@ -13,15 +13,16 @@ * @link https://github.com/jcchikikomori/hello-php */ - // checking requirements first before using +namespace controllers; + require_once "classes/App.php"; require_once "classes/Auth.php"; require_once "classes/User.php"; require_once "classes/concerns/RememberMe.php"; require_once "classes/handlers/URI.php"; -$context = new classes\Auth(); -$user = new classes\User(); +$context = new \classes\Auth(); +$user = new \classes\User(); // Immediate Password Reset Action if (isset($_GET['resetpasswordwithcode'])) { diff --git a/controllers/home.old.php b/controllers/home.old.php new file mode 100644 index 0000000..e5057c6 --- /dev/null +++ b/controllers/home.old.php @@ -0,0 +1,24 @@ + + * @license http://opensource.org/licenses/MIT MIT License + * @version TBA + * @link https://github.com/jcchikikomori/hello-php + */ + +class Home +{ + public function __construct() + { + echo "Coming Soon!"; + } +} diff --git a/controllers/home.php b/controllers/home.php index e5057c6..c40bfef 100644 --- a/controllers/home.php +++ b/controllers/home.php @@ -1,9 +1,7 @@ collectResponse(array($context)); +// if user logged in (using Auth class) +if ($context->isUserLoggedIn()) { + // put data here using App's render() + // put "auth" to show $auth on output, otherwise undefined + $context->render("templates/partials/logged_in"); +} +// not logged in +elseif (!$context->isUserLoggedIn()) { + $context->render("templates/partials/login_form"); } diff --git a/logout.php b/controllers/logout.php similarity index 73% rename from logout.php rename to controllers/logout.php index b5cb3a6..7fed3eb 100644 --- a/logout.php +++ b/controllers/logout.php @@ -1,3 +1,6 @@ $context->getUserTypes()]; diff --git a/home.php b/home.php deleted file mode 100644 index debdabd..0000000 --- a/home.php +++ /dev/null @@ -1,35 +0,0 @@ - - * @license http://opensource.org/licenses/MIT MIT License - * @version TBA - * @link https://github.com/jcchikikomori/hello-php - */ - -require_once "classes/App.php"; -require_once "classes/Auth.php"; -require_once "classes/concerns/RememberMe.php"; -require_once "classes/handlers/URI.php"; - -$context = new classes\Auth(); - -// collect response from Auth constructor -// Note: Auth was extended from App class so we can call functions from the App class -$context->collectResponse(array($context)); -// if user logged in (using Auth class) -if ($context->isUserLoggedIn()) { - // put data here using App's render() - // put "auth" to show $auth on output, otherwise undefined - $context->render("templates/partials/logged_in"); -} -// not logged in -elseif (!$context->isUserLoggedIn()) { - $context->render("templates/partials/login_form"); -} diff --git a/libraries/Renderer.php b/libraries/Renderer.php index 962f969..dfc049c 100644 --- a/libraries/Renderer.php +++ b/libraries/Renderer.php @@ -6,9 +6,9 @@ /** * Renderer - * + * * PHP Version 7.2 - * + * * @category Renderer * @package hello-php * @author John Cyrill Corsanes @@ -32,5 +32,5 @@ public static function render_partial($part, $data = array()) } else { throw new Exception("Unable to locate the context."); } - } -} \ No newline at end of file + } +}