Added: MVC structure, Router, Views and Controllers for home page and login/signup

This commit is contained in:
Filip Rojek 2024-12-25 23:02:09 +01:00
parent 7b4b349816
commit 3144078860
17 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,19 @@
<?php
class AuthController {
public function signin() {
$view = new View();
$data = [
'title' => 'Log In'
];
$view->render('auth/signin', $data);
}
public function signup() {
$view = new View();
$data = [
'title' => 'Register'
];
$view->render('auth/signup', $data);
}
}

View File

@ -0,0 +1,22 @@
<?php
class HomeController {
//private function render($view) {
// ob_start();
// require_once views . $view;
// $content = ob_get_clean();
// require_once views . 'layouts/base.php';
//}
public function index() {
$view = new View();
$data = [
'title' => 'Home'
];
$view->render('home/index', $data);
//require_once views . 'home/index.php';
}
public function home() {
$this->index();
}
}

View File

@ -0,0 +1,9 @@
<section class="signin">
<form>
<label for="username">Username</label>
<input type="text" name="username" id="username">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<input type="submit" value="Sign In" id="submit-signin">
</form>
</section>

11
app/views/auth/signup.php Normal file
View File

@ -0,0 +1,11 @@
<section class="signup">
<form>
<label for="username">Username</label>
<input type="text" name="username" id="username">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<label for="password-2">Password again</label>
<input type="password" name="password-2" id="password-2">
<input type="submit" value="Sign Up" id="submit-signup">
</form>
</section>

View File

13
app/views/errors/404.php Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Habit Tracker | Error 494</title>
</head>
<body>
<h1>Error 404 - Page not found</h1>
<a href="/">Go back home</a>
</body>
</html>

View File

View File

4
app/views/home/index.php Normal file
View File

@ -0,0 +1,4 @@
<div>
<h1>Welcome to Habit Tracker!</h1>
<p>Track your habits and achieve your goals.</p>
</div>

View File

@ -0,0 +1,16 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Habit Tracker | <?= $data['title'] ?></title>
</head>
<body>
<header>
<a href="/auth/signin">Log In</a>
<a href="/auth/signup">Sign Up</a>
</header>
<section class="content">
<?= $content ?>
</section>
</body>
</html>

View File

View File

@ -0,0 +1,4 @@
<header>
<a href="/signin">Log In</a>
<a href="/signup">Sign Up</a>
</header>

0
core/Controller.php Normal file
View File

28
core/Router.php Normal file
View File

@ -0,0 +1,28 @@
<?php
class Router {
private $routes = [];
public function add($route, $action) {
$this->routes[$route] = $action;
}
public function dispatch() {
$uri = $_SERVER['REQUEST_URI'];
$uri = parse_url($uri, PHP_URL_PATH);
if (array_key_exists($uri, $this->routes)) {
$action = $this->routes[$uri];
list($controllerName, $methodName) = explode('@', $action);
require_once controllers . "{$controllerName}.php";
$controller = new $controllerName();
$controller->$methodName();
} else {
http_response_code(404);
$view = new View();
$view->render('errors/404');
}
}
}

17
core/View.php Normal file
View File

@ -0,0 +1,17 @@
<?php
class View
{
public function render($view, $data = [], $layout = 'base')
{
// Extract variables to be accessible in the view
extract($data);
// Capture the view content
ob_start();
require_once views . $view . '.php';
$content = ob_get_clean();
// Include the base layout and inject the view content
require_once views . "layouts/$layout.php";
}
}

23
public/index.php Normal file
View File

@ -0,0 +1,23 @@
<?php
// Show errors and warnings
ini_set('display_errors', '1');
ini_set('log_errors', '1');
ini_set('error_log', '../storage/logs/error_log.log');
define('models', __DIR__ . '/../app/models/');
define('views', __DIR__ . '/../app/views/');
define('controllers', __DIR__ . '/../app/controllers/');
require_once '../core/Router.php';
require_once '../core/View.php';
require_once '../core/Controller.php';
// Initialize router
$router = new Router();
$router->add('/', 'HomeController@index');
$router->add('/home', 'HomeController@home');
// auth routes
$router->add('/auth/signin', 'AuthController@signin');
$router->add('/auth/signup', 'AuthController@signup');
$router->dispatch();

1
storage/logs/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.log