This commit is contained in:
William 2022-01-15 11:26:02 +01:00
parent 716dea87b2
commit 5d46a8b24d
17 changed files with 105 additions and 137 deletions

View File

@ -1,23 +0,0 @@
<?php
// This is the first step towards actually doing work with the database
// Encapsulate a single connection to a particular database
Class DatabaseHandler {
public function connect(): object {
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_PERSISTENT => true,
];
try {
return new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}
}

View File

@ -1,24 +0,0 @@
<?php
class Template {
// Generates a dynamic URL to the given path.
private function urlFor($path): string
{
// TODO: Make this WORK!!
$url = dirname(dirname(str_replace(realpath($_SERVER['DOCUMENT_ROOT']), '', realpath(__DIR__))));
if ((strlen($url) > 1)) {
$url = htmlspecialchars($url . '/');
}
//FUUUUUCKKK
echo($_SERVER['DOCUMENT_ROOT'] . ' ' . __DIR__);
die();
return($url.$path);
}
public function render($name): void
{
require(__DIR__ . '/../template/' . $name . '.php');
}
}

View File

@ -1,9 +0,0 @@
<?php
// This is the main file to be included on every page
session_start();
spl_autoload_register(function ($class_name) {
require(__DIR__ . '/class/' . $class_name . '.php');
});
$template = new Template();

View File

@ -0,0 +1,7 @@
<?php
// Extended controller for RESTful APIs
class ApiController extends Controller
{
// TODO: ...
}

20
app/core/Controller.php Normal file
View File

@ -0,0 +1,20 @@
<?php
class Controller
{
public function model(string $model): object
{
// Require model file
require __DIR__ . '/../model/' . $model . '.php';
// Instantiate model
return new $model();
}
public function view(string $view, array $data = []): void
{
// Import variables into the current symbol table from an array
extract($data);
// Require view file
require __DIR__ . '/../view/' . $view . '.php';
}
}

31
app/core/Database.php Normal file
View File

@ -0,0 +1,31 @@
<?php
// Encapsulates a single connection to a database
class Database
{
public object $dbh;
private string $host = '127.0.0.1';
private string $db = 'test';
private string $user = 'root';
private string $pass = '';
private string $charset = 'utf8mb4';
public function __construct()
{
try {
$this->dbh = $this->connectWithMySQL();
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int)$e->getCode());
}
}
private function connectWithMySQL(): object
{
$dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
$options = [
PDO::ATTR_PERSISTENT => true,
];
return new PDO($dsn, $this->user, $this->pass, $options);
}
}

14
app/core/Session.php Normal file
View File

@ -0,0 +1,14 @@
<?php
// Handles anything to do with sessions
class Session
{
// TODO: ...
public function __construct()
{
if (session_status() === PHP_SESSION_NONE)
{
session_start();
}
}
}

9
app/inc.php Normal file
View File

@ -0,0 +1,9 @@
<?php
/*
This is the main file to be included on every page.
It will act as a front controller of our application.
*/
spl_autoload_register(function ($class_name) {
require __DIR__ . '/core/' . $class_name . '.php';
});

8
app/model/Index.php Normal file
View File

@ -0,0 +1,8 @@
<?php
class Index {
public function getUsername(): string
{
return 'William';
}
}

3
app/view/index.php Normal file
View File

@ -0,0 +1,3 @@
<h1>Hello world</h1>
<p>This is a message from the view</p>
<p><?=$data?></p>

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?=htmlspecialchars($this->params["title"]);?> Stafett for livet</title> <title><?=htmlspecialchars($title);?> Stafett for livet</title>
<link rel="stylesheet" href="<?=$this->urlFor('static/style/main.css');?>"> <link rel="stylesheet" href="<?=$this->urlFor('static/style/main.css');?>">
<link rel="icon" href="<?=$this->urlFor('static/img/cropped-kf-propell-ikon-32x32.png');?>" sizes="32x32"> <link rel="icon" href="<?=$this->urlFor('static/img/cropped-kf-propell-ikon-32x32.png');?>" sizes="32x32">

View File

@ -1,15 +0,0 @@
<?php
// This file should serve as an example showing current implemented features.
require("../app/common.php");
// Include the top of page
$template->render("header");
?>
<h1>Eksempel side</h1>
<p>Dette er en eksempel side</p>
<?php
// Include the bottom of the page
$template->render("footer");
?>

View File

@ -1,13 +1,16 @@
<?php <?php
require("../app/common.php"); require("../app/inc.php");
$template->render("header"); // Instantiate a new controller
?> $contr = new Controller;
<h1>Forsida!</h1> // Grab the model
<p>Vi planlegger for Kreftforeningens Stafett for livet Kristiansand i juni 2021 og håper du blir med.</p> $model = $contr->model('Index');
<p>Stafettene i 2021 tilpasses nasjonale og lokale smittevernsråd, og det vil være lokale variasjoner i hvordan stafettene gjennomføres. Men målet er likt: Vi skal hedre fighterne, vise vår støtte, minnes de vi har mistet, gi håp og feire livet. </p>
<?php // Perform database task with model
$template->render("footer"); $username = $model->getUsername();
?>
// Display view with retrieved data
$contr->view('index', [
"data" => $username
]);

View File

@ -1,25 +0,0 @@
<?php
require("../app/common.php");
$template->render("header");
?>
<h1>Logg inn</h1>
<p>Fyll ut legitimasjonen din for å logge inn.</p>
<form method="post">
<label for="username">Brukernavn:</label>
<br>
<input type="text" id="username" name="username">
<br>
<label for="password">Passord:</label>
<br>
<input type="password" id="password" name="password">
<br>
<br>
<input type="submit" value="Bekreft">
</form>
<?php
$template->render("footer");
?>

View File

@ -1,16 +0,0 @@
<?php
// Initialize the session
session_start();
// Unset all of the session variables
session_unset();
// Destroy the session.
session_destroy();
session_start();
// Redirect to login page
header("location: login.php");
exit;
?>

View File

@ -1,15 +0,0 @@
<?php
// This file should serve as an example showing current implemented features.
require("../../app/common.php");
// Include the top of page
$template->render("header");
?>
<h1>Eksempel side</h1>
<p>Dette er en eksempel side</p>
<?php
// Include the bottom of the page
$template->render("footer");
?>