Skip to content

Commit

Permalink
Initial implementation of URI Handling with Controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchikikomori committed Oct 16, 2023
1 parent e4cc399 commit 2f60260
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 104 deletions.
87 changes: 38 additions & 49 deletions classes/handlers/URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions forgotpassword.php → controllers/forgotpassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
24 changes: 24 additions & 0 deletions controllers/home.old.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace controllers;

/**
* The Application's Home Controller
*
* PHP version 8.1
*
* @category Home
* @package hello-php
* @author John Cyrill Corsanes <jccorsanes@protonmail.com>
* @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!";
}
}
31 changes: 22 additions & 9 deletions controllers/home.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

namespace controllers;

/**
* The Application's Home Controller
* The Application's Home Page or Root Directory
*
* PHP version 8.1
*
Expand All @@ -15,10 +13,25 @@
* @link https://github.com/jcchikikomori/hello-php
*/

class Home
{
public function __construct()
{
echo "Coming Soon!";
}
namespace controllers;

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");
}
3 changes: 3 additions & 0 deletions logout.php → controllers/logout.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<?php

namespace controllers;

header("Location: " . DIRECTORY_SEPARATOR . "?logout");
die();
4 changes: 3 additions & 1 deletion multiuser.php → controllers/multiuser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @link https://github.com/jcchikikomori/hello-php
*/

namespace controllers;

// Core components first such as main classes then load dependencies
// then Instantiate the class to use it
require_once "classes/App.php";
Expand All @@ -22,7 +24,7 @@
require_once "classes/handlers/URI.php";

// load the auth class then instantiate again
$context = new classes\Auth();
$context = new \classes\Auth();

// either multi-user login or switch user (requires multi-user too)
if (
Expand Down
4 changes: 3 additions & 1 deletion register.php → controllers/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
* @link https://github.com/jcchikikomori/hello-php
*/

namespace controllers;

require_once "classes/App.php";
require_once "classes/Auth.php";
require_once "classes/Registration.php";
require_once "classes/concerns/RememberMe.php";
require_once "classes/handlers/URI.php";

$context = new classes\Registration();
$context = new \classes\Registration();

// Now put your data here and include in render()
$data = ['user_types' => $context->getUserTypes()];
Expand Down
35 changes: 0 additions & 35 deletions home.php

This file was deleted.

8 changes: 4 additions & 4 deletions libraries/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

/**
* Renderer
*
*
* PHP Version 7.2
*
*
* @category Renderer
* @package hello-php
* @author John Cyrill Corsanes <jccorsanes@protonmail.com>
Expand All @@ -32,5 +32,5 @@ public static function render_partial($part, $data = array())
} else {
throw new Exception("Unable to locate the context.");
}
}
}
}
}

0 comments on commit 2f60260

Please sign in to comment.